1 18 19 package org.objectweb.util.monolog.wrapper.log4j; 20 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.MonologFactory; 23 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter; 24 import org.apache.log4j.PatternLayout; 25 import org.apache.log4j.ConsoleAppender; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.io.OutputStreamWriter ; 30 31 35 public class ConsoleHandler extends ConsoleAppender implements Handler { 36 37 40 protected HashMap prop = null; 41 42 public ConsoleHandler() { 43 super(); 44 } 45 46 50 public ConsoleHandler(String name) { 51 super(); 52 setName(name); 53 prop = new HashMap (); 54 } 55 56 public Map getAttributes() { 57 return prop; 58 } 59 60 public void setAttributes(Map attributes) { 61 prop.clear(); 62 prop.putAll(attributes); 63 Object mf = prop.get("activation"); 64 if (mf != null) { 65 prop.remove("activation"); 66 setAttribute("activation", mf); 67 } 68 } 69 70 73 public String getType() { 74 return "console"; 75 } 76 77 public String [] getAttributeNames() { 78 return (String []) prop.keySet().toArray(new String [0]); 79 } 80 81 public Object getAttribute(String key) { 82 return prop.get(key); 83 } 84 85 public Object setAttribute(String key, Object value) { 86 if (prop == null) 87 prop = new HashMap (); 88 if (!key.equalsIgnoreCase("activation")) { 89 return prop.put(key, value); 90 } else if (prop.containsKey(key)) { 91 return null; } 93 MonologFactory mf = (MonologFactory) value; 94 String pattern = (String ) prop.get(Handler.PATTERN_ATTRIBUTE); 95 if (pattern != null) { 96 setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern))); 97 } 98 String level = (String ) prop.get(Handler.LEVEL_ATTRIBUTE); 99 if (level != null && level.length() > 0) { 100 int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf); 101 setThreshold(org.apache.log4j.Level.toLevel(levelVal)); 102 } 103 String output = (String ) prop.get(Handler.OUTPUT_ATTRIBUTE); 104 output = RelatifEnvironmentPathGetter.getRealPath(output); 105 if (output != null) { 106 super.target = output; 107 if (output.equalsIgnoreCase("System.out")) { 108 setWriter(new OutputStreamWriter (System.out)); 109 } else if (output.equalsIgnoreCase("System.err")) { 110 setWriter(new OutputStreamWriter (System.err)); 111 } 112 } 113 super.activateOptions(); 114 return null; 115 } 116 117 } 118 119 | Popular Tags |