1 18 19 package org.objectweb.util.monolog.wrapper.javaLog; 20 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.MonologFactory; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter; 25 26 import java.io.UnsupportedEncodingException ; 27 import java.util.logging.Filter ; 28 import java.util.logging.Formatter ; 29 import java.util.logging.LogRecord ; 30 import java.util.logging.FileHandler ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 34 39 public class GenericHandler 40 extends java.util.logging.Handler 41 implements Handler { 42 43 46 public java.util.logging.Handler handler = null; 47 48 52 protected String type; 53 54 57 protected String name; 58 59 62 Map attributes = null; 63 64 public GenericHandler() { 65 attributes = new HashMap (); 66 } 67 68 public GenericHandler(String name) { 69 this(); 70 this.name = name; 71 } 72 73 79 public GenericHandler(String name, String type) { 80 this(name); 81 this.type = type; 82 } 83 84 89 public GenericHandler(String name, java.util.logging.Handler h) { 90 this(name); 91 handler = h; 92 if (h instanceof FileHandler ) { 93 type = "file"; 94 } else if (h instanceof ConsoleHandler) { 95 type = "console"; 96 } 97 } 98 99 102 public String getName() { 103 return name; 104 } 105 106 109 public void setName(String name) { 110 this.name = name; 111 } 112 113 116 public String getType() { 117 return type; 118 } 119 120 123 public String [] getAttributeNames() { 124 return (String []) attributes.keySet().toArray(new String [0]); 125 } 126 127 131 public Object getAttribute(String n) { 132 return attributes.get(n); 133 } 134 135 141 public Object setAttribute(String _name, Object value) { 142 if (!_name.equalsIgnoreCase("activation")) { 143 return attributes.put(_name, value); 144 } 145 if (type == null) { 146 type = (String ) attributes.get("handlertype"); 147 } 148 MonologFactory mf = (MonologFactory) value; 149 String output = (String ) attributes.get(Handler.OUTPUT_ATTRIBUTE); 150 output = RelatifEnvironmentPathGetter.getRealPath(output); 151 String pattern = (String ) attributes.get(Handler.PATTERN_ATTRIBUTE); 152 String level = (String ) attributes.get(Handler.LEVEL_ATTRIBUTE); 153 String append = (String ) attributes.get(Handler.APPEND_MODE_ATTRIBUTE); 154 String nbfile = (String ) attributes.get(Handler.FILE_NUMBER_ATTRIBUTE); 155 String fileSize = (String ) attributes.get(Handler.MAX_SIZE_ATTRIBUTE); 156 boolean appendVal = true; 157 if (append != null && append.length() > 0) { 158 appendVal = Boolean.getBoolean(append); 159 } 160 int levelVal = BasicLevel.DEBUG; 161 if (level != null && level.length() > 0) { 162 levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf); 163 } 164 if ("console".equalsIgnoreCase(type)) { 165 handler = new ConsoleHandler(); 166 if (output != null && output.length() >0) { 167 if (output.equalsIgnoreCase("System.err")) { 168 ((ConsoleHandler) handler).setOutput(System.err); 169 } else if (output.equalsIgnoreCase("switch")) { 170 ((ConsoleHandler) handler).activateSwitching(); 171 } else if (output.equalsIgnoreCase("System.out")) { 172 ((ConsoleHandler) handler).setOutput(System.out); 173 } 174 } 175 } else if ("file".equalsIgnoreCase(type) 176 || "rollingfile".equalsIgnoreCase(type)) { 177 int limit = 0; 178 if (fileSize != null && fileSize.length() > 0) { 179 limit = Integer.parseInt(fileSize); 180 } 181 int count = 1; 182 if (nbfile != null && nbfile.length() > 0) { 183 count = Integer.parseInt(nbfile); 184 } 185 try { 186 handler = new FileHandler (output, limit, count, appendVal); 187 } catch (Exception e) { 188 throw new IllegalStateException ("Error when building the handler '" 189 + name + "': " + e.getMessage()); 190 } 191 } else { 192 throw new IllegalStateException ("Error when building the handler '" 193 + name + "': unknwon type: " + type); 194 } 195 handler.setFormatter(new MonologFormatter(pattern)); 196 handler.setLevel(LevelImpl.int2Level(levelVal)); 197 return null; 198 } 199 200 201 204 207 public void close() { 208 handler.close(); 209 } 210 211 214 public void flush() { 215 handler.flush(); 216 } 217 218 221 public String getEncoding() { 222 return handler.getEncoding(); 223 } 224 225 228 public Filter getFilter() { 229 return handler.getFilter(); 230 } 231 232 235 public Formatter getFormatter() { 236 return handler.getFormatter(); 237 } 238 239 242 public java.util.logging.Level getLevel() { 243 System.out.println("handler("+ name + ").getLevel(): " + handler.getLevel().getName()); 244 return handler.getLevel(); 245 } 246 247 250 public boolean isLoggable(LogRecord record) { 251 return handler.isLoggable(record); 253 } 254 255 258 public void publish(LogRecord record) { 259 handler.publish(record); 261 } 262 263 266 public void setEncoding(String encoding) 267 throws SecurityException , UnsupportedEncodingException { 268 handler.setEncoding(encoding); 269 } 270 271 274 protected void setException(Exception exception) { 275 } 276 277 280 public void setFilter(Filter newFilter) { 281 handler.setFilter(newFilter); 282 } 283 284 287 public void setFormatter(Formatter newFormatter) { 288 handler.setFormatter(newFormatter); 289 } 290 291 295 public void setLevel(java.util.logging.Level newLevel) { 296 handler.setLevel(newLevel); 297 } 298 299 } 300 | Popular Tags |