1 29 30 package com.caucho.log; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.RawString; 34 import com.caucho.jmx.Jmx; 35 import com.caucho.loader.CloseListener; 36 import com.caucho.loader.Environment; 37 import com.caucho.util.L10N; 38 import com.caucho.vfs.WriteStream; 39 40 import javax.annotation.PostConstruct; 41 import java.util.ArrayList ; 42 import java.util.logging.Formatter ; 43 import java.util.logging.Handler ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 50 public class LogConfig extends RotateLog { 51 private static final L10N L = new L10N(LogConfig.class); 52 53 private ArrayList <Handler > _handlers; 54 private Formatter _formatter; 55 private String _timestamp; 56 57 private ArrayList <SubLogger> _subLoggers = new ArrayList <SubLogger>(); 58 private SubLogger _subLogger; 59 60 private String _mbeanName; 61 private boolean _isInit; 62 63 public LogConfig() 64 { 65 setTimestamp("[%Y/%m/%d %H:%M:%S.%s] "); 66 } 67 68 73 public void setId(String name) 74 { 75 if (name.equals("/")) 77 name = ""; 78 79 setName(name); 80 } 81 82 85 public void setName(String name) 86 { 87 getSubLogger().setName(name); 88 } 89 90 93 public String getName() 94 { 95 return getSubLogger().getName(); 96 } 97 98 101 public void setMBeanName(String name) 102 { 103 _mbeanName = name; 104 } 105 106 109 public void setUseParentHandlers(boolean useParentHandlers) 110 throws ConfigException 111 { 112 getSubLogger().setUseParentHandlers(useParentHandlers); 113 } 114 115 118 public void setLevel(String level) 119 throws ConfigException 120 { 121 getSubLogger().setLevel(level); 122 123 if (_isInit && _handlers != null) { 124 for (Handler handler : _handlers) { 125 handler.setLevel(getSubLogger().getLevel()); 126 } 127 } 128 129 130 } 131 132 135 public String getLevel() 136 { 137 Level level = getSubLogger().getLevel(); 138 139 if (level != null) 140 return level.getName(); 141 else 142 return Level.INFO.getName(); 143 } 144 145 148 private SubLogger getSubLogger() 149 { 150 if (_subLogger == null) { 151 _subLogger = new SubLogger(); 152 _subLoggers.add(_subLogger); 153 } 154 155 return _subLogger; 156 } 157 158 161 public void setTimestamp(String timestamp) 162 { 163 _timestamp = timestamp; 164 } 165 166 170 public void setFormat(RawString format) 171 { 172 if (_formatter == null) { 173 _formatter = new ELFormatter(); 174 } 175 176 if (_formatter instanceof ELFormatter) 177 ((ELFormatter)_formatter).setFormat(format); 178 } 179 180 184 public String getFormat() 185 { 186 if (_formatter != null && _formatter instanceof ELFormatter) 187 return ((ELFormatter)_formatter).getFormat(); 188 else 189 return null; 190 } 191 192 195 public void setFormatter(Formatter formatter) 196 { 197 _formatter = formatter; 198 } 199 200 203 public void addHandler(Handler handler) 204 { 205 if (handler == null) 206 throw new NullPointerException (); 207 208 if (_handlers == null) 209 _handlers = new ArrayList <Handler >(); 210 211 _handlers.add(handler); 212 } 213 214 217 public String getTagName() 218 { 219 return "log"; 220 } 221 222 225 public SubLogger createLogger() 226 { 227 SubLogger subLogger = new SubLogger(); 228 229 subLogger.setLevel("info"); 230 _subLoggers.add(subLogger); 231 232 return subLogger; 233 } 234 235 238 @PostConstruct 239 public void init() 240 throws ConfigException, java.io.IOException 241 { 242 _isInit = true; 243 244 if (_handlers == null) 245 super.init(); 246 247 if (_subLoggers.size() == 0) 248 getSubLogger(); 249 250 if (_subLogger != null) 251 _subLogger.init(); 252 253 if (_formatter instanceof ELFormatter) { 254 ((ELFormatter)_formatter).init(); 255 } 256 261 262 WriteStream os = null; 263 264 if (_handlers == null) { 265 os = getRotateStream().getStream(); 266 267 if (_timestamp != null) { 268 TimestampFilter filter = new TimestampFilter(); 269 filter.setTimestamp(_timestamp); 270 filter.setStream(os); 271 os = new WriteStream(filter); 272 } 273 274 String encoding = System.getProperty("file.encoding"); 275 276 if (encoding != null) 277 os.setEncoding(encoding); 278 279 os.setDisableClose(true); 280 } 281 282 boolean hasCloseListener = false; 283 for (int i = 0; i < _subLoggers.size(); i++) { 284 SubLogger subLogger = _subLoggers.get(i); 285 Logger logger = subLogger.getLogger(); 286 287 Level subLevel = subLogger.getLevel(); 288 Level level = logger.getLevel(); 289 290 if (subLevel != null 291 && (level == null && subLevel.intValue() < Level.INFO.intValue() 292 || level != null && subLevel.intValue() < level.intValue())) { 293 logger.setLevel(subLogger.getLevel()); 294 } 295 296 if (_handlers == null) { 297 StreamHandler handler = new StreamHandler(os); 298 handler.setFormatter(_formatter); 299 300 _handlers = new ArrayList <Handler >(); 301 _handlers.add(handler); 302 } 303 304 for (int j = 0; j < _handlers.size(); j++) { 305 SubHandler subHandler = new SubHandler(_handlers.get(j)); 306 307 if (subLogger.getLevel() != null) 308 subHandler.setLevel(subLogger.getLevel()); 309 310 if (! (logger instanceof EnvironmentLogger)) { 311 CloseListener listener = new CloseListener(subHandler); 312 313 Environment.addClassLoaderListener(listener); 314 } 315 316 logger.addHandler(subHandler); 317 } 318 } 319 320 if (_mbeanName != null) { 321 try { 322 Jmx.register(this, Jmx.getObjectName(_mbeanName)); 323 } catch (Throwable e) { 324 } 325 } 326 } 327 328 static Level toLevel(String level) 329 throws ConfigException 330 { 331 if (level.equals("off")) 332 return Level.OFF; 333 else if (level.equals("severe")) 334 return Level.SEVERE; 335 else if (level.equals("warning")) 336 return Level.WARNING; 337 else if (level.equals("info")) 338 return Level.INFO; 339 else if (level.equals("config")) 340 return Level.CONFIG; 341 else if (level.equals("fine")) 342 return Level.FINE; 343 else if (level.equals("finer")) 344 return Level.FINER; 345 else if (level.equals("finest")) 346 return Level.FINEST; 347 else if (level.equals("all")) 348 return Level.ALL; 349 else 350 throw new ConfigException(L.l("`{0}' is an unknown log level. Log levels are:\noff - disable logging\nsevere - severe errors only\nwarning - warnings\ninfo - information\nconfig - configuration\nfine - fine debugging\nfiner - finer debugging\nfinest - finest debugging\nall - all debugging", level)); 351 } 352 353 public static class SubLogger { 354 private Logger _logger; 355 private String _name = ""; 356 private Level _level; 357 private boolean _useParentHandlers = true; 358 359 private Handler _handler; 361 362 365 public void setId(String name) 366 { 367 if (name.equals("/")) 369 name = ""; 370 371 setName(name); 372 } 373 374 377 public void setName(String name) 378 { 379 _name = name; 380 } 381 382 385 public String getName() 386 { 387 return _name; 388 } 389 390 393 public void setUseParentHandlers(boolean useParentHandlers) 394 throws ConfigException 395 { 396 _logger = Logger.getLogger(_name); 397 398 _logger.setUseParentHandlers(useParentHandlers); 399 400 _useParentHandlers = useParentHandlers; 401 } 402 403 406 public Logger getLogger() 407 { 408 return Logger.getLogger(_name); 409 } 410 411 414 public Level getLevel() 415 { 416 return _level; 417 } 418 419 422 public void setLevel(String level) 423 throws ConfigException 424 { 425 if (level.equals("off")) 426 _level = Level.OFF; 427 else if (level.equals("severe")) 428 _level = Level.SEVERE; 429 else if (level.equals("warning")) 430 _level = Level.WARNING; 431 else if (level.equals("info")) 432 _level = Level.INFO; 433 else if (level.equals("config")) 434 _level = Level.CONFIG; 435 else if (level.equals("fine")) 436 _level = Level.FINE; 437 else if (level.equals("finer")) 438 _level = Level.FINER; 439 else if (level.equals("finest")) 440 _level = Level.FINEST; 441 else if (level.equals("all")) 442 _level = Level.ALL; 443 else 444 throw new ConfigException(L.l("`{0}' is an unknown log level. Log levels are:\noff - disable logging\nsevere - severe errors only\nwarning - warnings\ninfo - information\nconfig - configuration\nfine - fine debugging\nfiner - finer debugging\nfinest - finest debugging\nall - all debugging", level)); 445 } 446 447 450 public void setHandler(Handler handler) 451 { 452 _handler = handler; 453 } 454 455 458 public Handler getHandler() 459 { 460 return _handler; 461 } 462 463 @PostConstruct 464 public void init() 465 throws ConfigException 466 { 467 if (_name == null) 468 throw new ConfigException(L.l("`name' is a required attribute.. Each logger must configure the log name, e.g. com.caucho.server.webapp.")); 469 } 470 } 471 } 472 | Popular Tags |