1 18 19 package org.objectweb.util.monolog.wrapper.log4j; 20 21 import org.apache.log4j.PatternLayout; 22 import org.apache.log4j.RollingFileAppender; 23 import org.objectweb.util.monolog.Monolog; 24 import org.objectweb.util.monolog.api.Handler; 25 import org.objectweb.util.monolog.api.MonologFactory; 26 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter; 27 28 import java.io.IOException ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 37 public class RollingFileHandler 38 extends RollingFileAppender 39 implements Handler { 40 41 44 protected HashMap prop = null; 45 46 public RollingFileHandler() { 47 super(); 48 } 49 50 55 public RollingFileHandler(String name) { 56 super(); 57 setName(name); 58 prop = new HashMap (); 59 } 60 61 public Map getAttributes() { 62 return prop; 63 } 64 65 public void setAttributes(Map attributes) { 66 prop.clear(); 67 prop.putAll(attributes); 68 Object mf = prop.get("activation"); 69 if (mf != null) { 70 prop.remove("activation"); 71 setAttribute("activation", mf); 72 } 73 } 74 75 78 public String getType() { 79 return "rollingfile"; 80 } 81 82 public String [] getAttributeNames() { 83 return (String []) prop.keySet().toArray(new String [0]); 84 } 85 86 public Object getAttribute(String key) { 87 return prop.get(key); 88 } 89 90 public Object setAttribute(String key, Object value) { 91 if (prop == null) { 92 prop = new HashMap (); 93 } 94 if (!key.equalsIgnoreCase("activation")) { 95 return prop.put(key, value); 96 } else if (prop.containsKey(key)) { 97 return null; } 99 MonologFactory mf = (MonologFactory) value; 100 String append = (String ) prop.get(Handler.APPEND_MODE_ATTRIBUTE); 101 if (append != null && append.length() > 0) { 102 fileAppend = Boolean.getBoolean(append); 103 } else { 104 fileAppend = true; 105 } 106 107 String maxSize = (String ) prop.get(Handler.MAX_SIZE_ATTRIBUTE); 108 if (maxSize != null && maxSize.length() > 0) { 109 setMaxFileSize(maxSize); 110 } 111 String nbFile = (String ) prop.get(Handler.FILE_NUMBER_ATTRIBUTE); 112 if (nbFile != null && nbFile.length() > 0) { 113 setMaxBackupIndex(Integer.valueOf(nbFile).intValue()); 114 } 115 116 String buffersize = (String ) prop.get(Handler.BUFFER_ATTRIBUTE); 117 if (buffersize != null && buffersize.length() > 0) { 118 try { 119 setBufferSize(Integer.valueOf(buffersize).intValue()); 120 } catch (NumberFormatException e) { 121 Monolog.error("Bad specified buffer size for the handler '" 122 + name + "': " + buffersize, e); 123 } 124 } 125 126 String output = (String ) prop.get(Handler.OUTPUT_ATTRIBUTE); 127 output = RelatifEnvironmentPathGetter.getRealPath(output); 128 try { 129 setFile(output, fileAppend, bufferedIO, bufferSize); 130 } catch (IOException e) { 131 Monolog.error("Error during the creation of the handler '" 132 + name + "': ", e); 133 } 134 135 String pattern = (String ) prop.get(Handler.PATTERN_ATTRIBUTE); 136 setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern))); 137 138 String level = (String ) prop.get(Handler.LEVEL_ATTRIBUTE); 139 if (level != null && level.length() > 0) { 140 int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf); 141 setThreshold(org.apache.log4j.Level.toLevel(levelVal)); 142 } 143 super.activateOptions(); 144 return null; 145 } 146 } 147 | Popular Tags |