1 package org.apache.jetspeed.services.logging; 2 3 18 19 import java.util.Iterator ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletContext ; 27 28 import org.apache.turbine.services.InitializationException; 30 import org.apache.turbine.services.TurbineBaseService; 31 import org.apache.turbine.services.TurbineServices; 32 import org.apache.turbine.services.resources.ResourceService; 33 import org.apache.turbine.services.resources.TurbineResources; 34 import org.apache.turbine.services.logging.LoggingService; 35 import org.apache.turbine.services.logging.LoggingConfig; 36 import org.apache.turbine.services.logging.Logger; 37 import org.apache.turbine.util.RunData; 38 import org.apache.turbine.Turbine; 39 40 52 public class JetspeedLoggingService 53 extends TurbineBaseService 54 implements LoggingService 55 { 56 59 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedLoggingService.class.getName()); 60 61 64 private HashMap loggers; 65 66 69 private Logger defaultLogger; 70 71 74 private Logger simpleLogger; 75 76 79 private ServletContext context = null; 80 81 84 private ResourceService resources = null; 85 86 public JetspeedLoggingService() 87 { 88 loggers = new HashMap (); 89 defaultLogger = null; 90 } 91 92 100 public void init() 101 throws InitializationException 102 { 103 ServletConfig conf = Turbine.getTurbineServletConfig(); 104 init(conf); 105 } 106 107 115 public void init(ServletConfig config) 116 throws InitializationException 117 { 118 context = config.getServletContext(); 119 120 defaultLogger = new BaseLogger(logger); 123 124 simpleLogger = defaultLogger; 125 126 internalInit(); 127 setInit(true); 128 } 129 130 133 public ResourceService getResources() 134 { 135 if (resources == null) 136 { 137 resources = TurbineResources 139 .getResources(TurbineServices.SERVICE_PREFIX 140 + LoggingService.SERVICE_NAME); 141 142 String webappRoot = context.getRealPath("/"); 145 resources.setProperty(Turbine.WEBAPP_ROOT, webappRoot); 146 } 147 return (resources); 148 } 149 150 153 private void internalInit() throws InitializationException 154 { 155 ResourceService props = getResources(); 156 if (props == null) 157 { 158 throw new InitializationException("LoggingService failed to " 159 + "get access to the properties for this service."); 160 } 161 162 String defaultLoggerName = props.getString(LoggingConfig.DEFAULT); 164 165 if (defaultLoggerName == null) 167 { 168 throw new InitializationException("LoggingService can't find " 169 + "default logger name in the configuration file."); 170 } 171 172 loggers.put(defaultLoggerName, defaultLogger); 174 175 if (defaultLogger == null) 177 { 178 throw new InitializationException("LoggingService can't find " 179 + "default logger in working loggers."); 180 } 181 } 182 183 187 public void shutdown() 188 { 189 if (!getInit()) 190 { 191 return; 192 } 193 194 for ( Iterator iter = loggers.entrySet().iterator(); iter.hasNext(); ) { 195 Map.Entry entry = (Map.Entry ) iter.next(); 196 ((Logger) entry.getValue()).shutdown(); 197 } 198 199 defaultLogger = simpleLogger; 200 201 } 203 204 207 public final Logger getLogger() 208 { 209 return defaultLogger; 210 } 211 212 215 public Logger getLogger(String logName) 216 { 217 Logger logger = (Logger) loggers.get(logName); 218 if (logger == null) 219 { 220 logger = new BaseLogger(JetspeedLogFactoryService.getLogger(logName)); 221 if (logger == null) 222 { 223 return defaultLogger; 224 } 225 else 226 { 227 loggers.put(logName, logger); 228 } 229 } 230 return logger; 231 } 232 233 236 public void setLogLevel(int level) 237 { 238 defaultLogger.setLogLevel(level); 239 } 240 241 244 public void setLogLevel(String logName, int level) 245 { 246 Logger logger = (Logger) loggers.get(logName); 247 if (logger != null) 248 { 249 logger.setLogLevel(level); 250 } 251 } 252 253 256 public void setFormat(String format) 257 { 258 defaultLogger.setFormat(format); 259 } 260 261 264 public void setFormat(String logName, String format) 265 { 266 Logger logger = (Logger) loggers.get(logName); 267 if (logger != null) 268 { 269 logger.setFormat(format); 270 } 271 } 272 273 277 public void debug(String message) 278 { 279 defaultLogger.debug(message); 280 } 281 282 286 public void debug(String message, Throwable t) 287 { 288 defaultLogger.debug(message, t); 289 } 290 291 295 public void debug(String logName, String message, Throwable t) 296 { 297 Logger logger = getLogger(logName); 298 if (logger != null) 299 { 300 logger.debug(message, t); 301 } 302 else 303 { 304 defaultLogger.debug("FROM logger:" + logName + ": " + message); 305 } 306 } 307 308 312 public void debug(String logName, String message) 313 { 314 Logger logger = getLogger(logName); 315 if (logger != null) 316 { 317 logger.debug(message); 318 } 319 else 320 { 321 defaultLogger.debug("FROM logger:" + logName + ": " + message); 322 } 323 } 324 325 329 public void debug(String message, RunData data) 330 { 331 defaultLogger.debug(message); 332 } 333 334 338 public void debug(String message, RunData data, Throwable t) 339 { 340 defaultLogger.debug(message, t); 341 } 342 343 347 public void debug(String logName, String message, RunData data, Throwable t) 348 { 349 Logger logger = getLogger(logName); 350 if (logger != null) 351 { 352 logger.debug(message, data, t); 353 } 354 else 355 { 356 defaultLogger.debug("FROM logger:" + logName + ": " + message); 357 } 358 } 359 360 364 public void debug(String logName, String message, RunData data) 365 { 366 Logger logger = getLogger(logName); 367 if (logger != null) 368 { 369 logger.debug(message, data); 370 } 371 else 372 { 373 defaultLogger.debug("FROM logger:" + logName + ": " + message); 374 } 375 } 376 377 381 public void info(String message) 382 { 383 defaultLogger.info(message); 384 } 385 386 390 public void info(String message, Throwable t) 391 { 392 defaultLogger.info(message, t); 393 } 394 395 399 public void info(String logName, String message) 400 { 401 Logger logger = getLogger(logName); 402 if (logger != null) 403 { 404 logger.info(message); 405 } 406 else 407 { 408 defaultLogger.info("FROM logger:" + logName + ": " + message); 409 } 410 } 411 412 416 public void info(String logName, String message, Throwable t) 417 { 418 Logger logger = getLogger(logName); 419 if (logger != null) 420 { 421 logger.info(message, t); 422 } 423 else 424 { 425 defaultLogger.info("FROM logger:" + logName + ": " + message); 426 } 427 } 428 429 433 public void info(String message, RunData data) 434 { 435 defaultLogger.info(message); 436 } 437 438 442 public void info(String message, RunData data, Throwable t) 443 { 444 defaultLogger.info(message, t); 445 } 446 447 451 public void info(String logName, String message, RunData data) 452 { 453 Logger logger = getLogger(logName); 454 if (logger != null) 455 { 456 logger.info(message, data); 457 } 458 else 459 { 460 defaultLogger.info("FROM logger:" + logName + ": " + message); 461 } 462 } 463 464 468 public void info(String logName, String message, RunData data, Throwable t) 469 { 470 Logger logger = getLogger(logName); 471 if (logger != null) 472 { 473 logger.info(message, data, t); 474 } 475 else 476 { 477 defaultLogger.info("FROM logger:" + logName + ": " + message); 478 } 479 } 480 481 485 public void warn(String message) 486 { 487 defaultLogger.warn(message); 488 } 489 490 494 public void warn(String message, Throwable t) 495 { 496 defaultLogger.warn(message, t); 497 } 498 499 503 public void warn(String logName, String message) 504 { 505 Logger logger = getLogger(logName); 506 if (logger != null) 507 { 508 logger.warn(message); 509 } 510 else 511 { 512 defaultLogger.warn("FROM logger:" + logName + ": " + message); 513 } 514 } 515 516 520 public void warn(String logName, String message, Throwable t) 521 { 522 Logger logger = getLogger(logName); 523 if (logger != null) 524 { 525 logger.warn(message, t); 526 } 527 else 528 { 529 defaultLogger.warn("FROM logger:" + logName + ": " + message); 530 } 531 } 532 533 537 public void warn(String message, RunData data) 538 { 539 defaultLogger.warn(message); 540 } 541 542 546 public void warn(String message, RunData data, Throwable t) 547 { 548 defaultLogger.warn(message, t); 549 } 550 551 555 public void warn(String logName, String message, RunData data) 556 { 557 Logger logger = getLogger(logName); 558 if (logger != null) 559 { 560 logger.warn(message, data); 561 } 562 else 563 { 564 defaultLogger.warn("FROM logger:" + logName + ": " + message); 565 } 566 } 567 568 572 public void warn(String logName, String message, RunData data, Throwable t) 573 { 574 Logger logger = getLogger(logName); 575 if (logger != null) 576 { 577 logger.warn(message, data, t); 578 } 579 else 580 { 581 defaultLogger.warn("FROM logger:" + logName + ": " + message); 582 } 583 } 584 585 589 public void error(String message) 590 { 591 defaultLogger.error(message); 592 } 593 594 598 public void error(String message, Throwable t) 599 { 600 defaultLogger.error(message, t); 601 } 602 603 607 public void error(String logName, String message) 608 { 609 Logger logger = getLogger(logName); 610 if (logger != null) 611 { 612 logger.error(message); 613 } 614 else 615 { 616 defaultLogger.error("FROM logger:" + logName + ": " + message); 617 } 618 } 619 620 624 public void error(String logName, String message, Throwable t) 625 { 626 Logger logger = getLogger(logName); 627 if (logger != null) 628 { 629 logger.error(message, t); 630 } 631 else 632 { 633 defaultLogger.error("FROM logger:" + logName + ": " + message); 634 } 635 } 636 637 641 public void error(String message, RunData data) 642 { 643 defaultLogger.error(message); 644 } 645 646 650 public void error(String message, RunData data, Throwable t) 651 { 652 defaultLogger.error(message, t); 653 } 654 655 659 public void error(String logName, String message, RunData data) 660 { 661 Logger logger = getLogger(logName); 662 if (logger != null) 663 { 664 logger.error(message, data); 665 } 666 else 667 { 668 defaultLogger.error("FROM logger:" + logName + ": " + message); 669 } 670 } 671 672 676 public void error(String logName, String message, RunData data, Throwable t) 677 { 678 Logger logger = getLogger(logName); 679 if (logger != null) 680 { 681 logger.error(message, data, t); 682 } 683 else 684 { 685 defaultLogger.error("FROM logger:" + logName + ": " + message); 686 } 687 } 688 } 689 | Popular Tags |