1 22 39 package org.objectweb.petals.util.monolog.wrapper.javalog; 40 41 import java.io.UnsupportedEncodingException ; 42 import java.util.HashMap ; 43 import java.util.Map ; 44 import java.util.logging.FileHandler ; 45 import java.util.logging.Filter ; 46 import java.util.logging.Formatter ; 47 import java.util.logging.LogRecord ; 48 49 import org.objectweb.util.monolog.api.BasicLevel; 50 import org.objectweb.util.monolog.api.Handler; 51 import org.objectweb.util.monolog.api.MonologFactory; 52 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter; 53 import org.objectweb.util.monolog.wrapper.javaLog.LevelImpl; 54 55 62 public class GenericHandler extends java.util.logging.Handler implements 63 Handler { 64 65 68 public java.util.logging.Handler handler = null; 69 70 73 protected String name; 74 75 79 protected String type; 80 81 84 Map <String , Object > attributes = null; 85 86 public GenericHandler() { 87 attributes = new HashMap <String , Object >(); 88 } 89 90 public GenericHandler(String name) { 91 this(); 92 this.name = name; 93 } 94 95 103 public GenericHandler(String name, java.util.logging.Handler h) { 104 this(name); 105 handler = h; 106 if (h instanceof FileHandler ) { 107 type = "file"; 108 } else if (h instanceof ConsoleHandler) { 109 type = "console"; 110 } 111 } 112 113 122 public GenericHandler(String name, String type) { 123 this(name); 124 this.type = type; 125 } 126 127 130 public void close() { 131 handler.close(); 132 } 133 134 137 public void flush() { 138 handler.flush(); 139 } 140 141 147 public Object getAttribute(String n) { 148 return attributes.get(n); 149 } 150 151 154 public String [] getAttributeNames() { 155 return (String []) attributes.keySet().toArray(new String [0]); 156 } 157 158 161 public String getEncoding() { 162 return handler.getEncoding(); 163 } 164 165 168 public Filter getFilter() { 169 return handler.getFilter(); 170 } 171 172 175 178 public Formatter getFormatter() { 179 return handler.getFormatter(); 180 } 181 182 186 public java.util.logging.Level getLevel() { 187 System.out.println("handler(" + name + ").getLevel(): " 188 + handler.getLevel().getName()); 189 return handler.getLevel(); 190 } 191 192 195 public String getName() { 196 return name; 197 } 198 199 202 public String getType() { 203 return type; 204 } 205 206 209 public boolean isLoggable(LogRecord record) { 210 return handler.isLoggable(record); 213 } 214 215 218 public void publish(LogRecord record) { 219 handler.publish(record); 222 } 223 224 233 public Object setAttribute(String _name, Object value) { 234 if (!_name.equalsIgnoreCase("activation")) { 235 return attributes.put(_name, value); 236 } 237 if (type == null) { 238 type = (String ) attributes.get("handlertype"); 239 } 240 MonologFactory mf = (MonologFactory) value; 241 String output = (String ) attributes.get(Handler.OUTPUT_ATTRIBUTE); 242 output = RelatifEnvironmentPathGetter.getRealPath(output); 243 String pattern = (String ) attributes.get(Handler.PATTERN_ATTRIBUTE); 244 String level = (String ) attributes.get(Handler.LEVEL_ATTRIBUTE); 245 String append = (String ) attributes.get(Handler.APPEND_MODE_ATTRIBUTE); 246 String nbfile = (String ) attributes.get(Handler.FILE_NUMBER_ATTRIBUTE); 247 String fileSize = (String ) attributes.get(Handler.MAX_SIZE_ATTRIBUTE); 248 boolean appendVal = true; 249 if (append != null && append.length() > 0) { 250 appendVal = Boolean.getBoolean(append); 251 } 252 int levelVal = BasicLevel.DEBUG; 253 if (level != null && level.length() > 0) { 254 levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl 255 .evaluate(level, mf); 256 } 257 if ("console".equalsIgnoreCase(type)) { 258 handler = new ConsoleHandler(); 259 if (output != null && output.length() > 0) { 260 if (output.equalsIgnoreCase("System.err")) { 261 ((ConsoleHandler) handler).setOutput(System.err); 262 } else if (output.equalsIgnoreCase("switch")) { 263 ((ConsoleHandler) handler).activateSwitching(); 264 } else if (output.equalsIgnoreCase("System.out")) { 265 ((ConsoleHandler) handler).setOutput(System.out); 266 } 267 } 268 } else if ("file".equalsIgnoreCase(type) 269 || "rollingfile".equalsIgnoreCase(type)) { 270 int limit = 0; 271 if (fileSize != null && fileSize.length() > 0) { 272 limit = Integer.parseInt(fileSize); 273 } 274 int count = 1; 275 if (nbfile != null && nbfile.length() > 0) { 276 count = Integer.parseInt(nbfile); 277 } 278 try { 279 handler = new FileHandler (output, limit, count, appendVal); 280 } catch (Exception e) { 281 throw new IllegalStateException ( 282 "Error when building the handler '" + name + "': " 283 + e.getMessage()); 284 } 285 } else { 286 throw new IllegalStateException ("Error when building the handler '" 287 + name + "': unknwon type: " + type); 288 } 289 handler.setFormatter(new MonologFormatter(pattern)); 290 handler.setLevel(LevelImpl.int2Level(levelVal)); 291 return null; 292 } 293 294 297 public void setEncoding(String encoding) throws SecurityException , 298 UnsupportedEncodingException { 299 handler.setEncoding(encoding); 300 } 301 302 305 public void setFilter(Filter newFilter) { 306 handler.setFilter(newFilter); 307 } 308 309 312 public void setFormatter(Formatter newFormatter) { 313 handler.setFormatter(newFormatter); 314 } 315 316 320 public void setLevel(java.util.logging.Level newLevel) { 321 handler.setLevel(newLevel); 322 } 323 324 327 public void setName(String name) { 328 this.name = name; 329 } 330 331 334 protected void setException(Exception exception) { 335 } 336 337 } 338 | Popular Tags |