1 18 19 package org.objectweb.util.monolog.wrapper.log4j; 20 21 import org.apache.log4j.FileAppender; 22 import org.apache.log4j.PatternLayout; 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 FileHandler extends FileAppender implements Handler { 38 39 42 protected HashMap prop = null; 43 44 public FileHandler() { 45 super(); 46 } 47 48 52 public FileHandler(String name) { 53 super(); 54 setName(name); 55 prop = new HashMap (); 56 } 57 58 public Map getAttributes() { 59 return prop; 60 } 61 62 public void setAttributes(Map attributes) { 63 prop.clear(); 64 prop.putAll(attributes); 65 Object mf = prop.get("activation"); 66 if (mf != null) { 67 prop.remove("activation"); 68 setAttribute("activation", mf); 69 } 70 } 71 72 75 public String getType() { 76 return "file"; 77 } 78 79 public String [] getAttributeNames() { 80 return (String []) prop.keySet().toArray(new String [0]); 81 } 82 83 public Object getAttribute(String key) { 84 return prop.get(key); 85 } 86 87 public Object setAttribute(String key, Object value) { 88 if (prop == null) { 89 prop = new HashMap (); 90 } 91 if (!key.equalsIgnoreCase("activation")) { 92 return prop.put(key, value); 93 } else if (prop.containsKey(key)) { 94 return null; } 96 MonologFactory mf = (MonologFactory) value; 97 String output = (String ) prop.get(Handler.OUTPUT_ATTRIBUTE); 98 output = RelatifEnvironmentPathGetter.getRealPath(output); 99 String append = (String ) prop.get(Handler.APPEND_MODE_ATTRIBUTE); 100 if (append != null && append.length() > 0) { 101 fileAppend = Boolean.getBoolean(append); 102 } else { 103 fileAppend = true; 104 } 105 106 String buffersize = (String ) prop.get(Handler.BUFFER_ATTRIBUTE); 107 if (buffersize != null && buffersize.length() > 0) { 108 try { 109 setBufferSize(Integer.valueOf(buffersize).intValue()); 110 } catch (NumberFormatException e) { 111 Monolog.error("Bad specified buffer size for the handler '" 112 + name + "': " + buffersize, e); 113 } 114 } 115 116 try { 117 setFile(output, fileAppend, bufferedIO, bufferSize); 118 } catch (IOException e) { 119 Monolog.error("Error during the creation of the handler '" 120 + name + "': ", e); 121 } 122 123 String pattern = (String ) prop.get(Handler.PATTERN_ATTRIBUTE); 124 setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern))); 125 126 String level = (String ) prop.get(Handler.LEVEL_ATTRIBUTE); 127 if (level != null && level.length() > 0) { 128 int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf); 129 setThreshold(org.apache.log4j.Level.toLevel(levelVal)); 130 } 131 132 super.activateOptions(); 133 return null; 134 } 135 136 } 137 | Popular Tags |