1 package org.apache.turbine.services.logging;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.Enumeration;
58 import java.util.Hashtable;
59 import java.util.Vector;
60 import javax.servlet.ServletConfig;
61 import javax.servlet.ServletContext;
62 import org.apache.turbine.services.InitializationException;
63 import org.apache.turbine.services.TurbineBaseService;
64 import org.apache.turbine.services.TurbineServices;
65 import org.apache.turbine.services.resources.ResourceService;
66 import org.apache.turbine.services.resources.TurbineResources;
67 import org.apache.turbine.util.RunData;
68 import org.apache.turbine.Turbine;
69
70 /***
71 * The default implementation of the logging service in Turbine.
72 *
73 * This service functions as a logger provider.
74 * It allows access to loggers: explicite by the getLogger method,
75 * or by printing methods (info, error...).
76 * Real work is done by classes that implement interface: Logger.
77 * The configuration of the service is read from the TurbineResources.properties.
78 * The rest of the configuration is done through a defined LoggingConfig class.
79 *
80 * Names of the loggers, classes, log levels, destinations are defined in that file.
81 *
82 * @see org.apache.turbine.services.logging.Logger
83 * @author <a href="mailto:Tomasz.Zielinski@e-point.pl">Tomasz Zielinski</a>
84 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
85 * @version $Id: TurbineLoggingService.java,v 1.3 2002/04/05 07:04:55 dobbs Exp $
86 */
87 public class TurbineLoggingService
88 extends TurbineBaseService
89 implements LoggingService
90 {
91 /*** loggers repository */
92 protected Hashtable loggersTable;
93
94 /*** logger for methods without target */
95 protected Logger defaultLogger;
96
97 /*** bootstrap and shutdown logger using context.log */
98 protected Logger simpleLogger;
99
100 /*** context for resolving paths and servlet logging */
101 protected ServletContext context = null;
102
103 /*** Resources for this Service */
104 private ResourceService resources = null;
105
106 /*** Name of the class that is cached for doing the reading
107 of the properties file for logging. */
108 private String loggingConfigClassName = null;
109
110 public TurbineLoggingService()
111 {
112 loggersTable = new Hashtable();
113 defaultLogger = null;
114 }
115
116 /***
117 * Inits the service using servlet parameters to obtain path to the
118 * configuration file. Change relatives paths.
119 */
120 public void init(ServletConfig config)
121 throws InitializationException
122 {
123 context = config.getServletContext();
124
125 // Create bootstrap logger, for handling exceptions during service
126 // initialization.
127 defaultLogger = new ServletLogger();
128
129 LoggingConfig lc = getLoggingConfig();
130 lc.setName(LoggingConfig.DEFAULT);
131 lc.setInitResource(null);
132 lc.setServletContext(context);
133 lc.init();
134
135 defaultLogger.init(lc);
136 simpleLogger = defaultLogger;
137
138 internalInit();
139 setInit(true);
140 }
141
142 /***
143 * Creates a new LoggingConfig object. By default, this
144 * is a PropertiesLoggingConfig object. You can override this
145 * by changing your TurbineResources.properties file to something
146 * else. Prop: services.TurbineLoggingService.loggingConfig
147 * FIXME: Not built for speed. :-(
148 */
149 public LoggingConfig getLoggingConfig()
150 throws InitializationException
151 {
152 if (loggingConfigClassName == null)
153 {
154 ResourceService props = getResources();
155 loggingConfigClassName = props.getString(LoggingConfig.LOGGINGCONFIG,
156 "org.apache.turbine.services.logging.PropertiesLoggingConfig" );
157 }
158
159 try
160 {
161 return (LoggingConfig)Class
162 .forName(loggingConfigClassName).newInstance();
163 }
164 catch (java.lang.InstantiationException ie)
165 {
166 throw new InitializationException(
167 "LoggingService: Failed to instantiate LoggingConfig: " +
168 loggingConfigClassName);
169 }
170 catch (java.lang.IllegalAccessException iae)
171 {
172 throw new InitializationException(
173 "LoggingService: Failed to instantiate LoggingConfig: " +
174 loggingConfigClassName);
175 }
176 catch (java.lang.ClassNotFoundException cnfe)
177 {
178 throw new InitializationException(
179 "LoggingService: Failed to instantiate LoggingConfig: " +
180 loggingConfigClassName);
181 }
182 }
183
184 /***
185 * This gets the ResourceService associated to this Service
186 */
187 public ResourceService getResources()
188 {
189 if (resources == null)
190 {
191 // Get the properties for this Service
192 resources = TurbineResources
193 .getResources(TurbineServices.SERVICE_PREFIX +
194 LoggingService.SERVICE_NAME);
195
196 //add webappRoot manually - cos logging is a primary
197 //service and so it is not yet defined
198 String webappRoot = context.getRealPath("");
199 resources.setProperty(Turbine.WEBAPP_ROOT,webappRoot);
200
201 }
202 return (resources);
203 }
204
205 /***
206 * This method initializes the service.
207 */
208 private void internalInit() throws InitializationException
209 {
210 ResourceService props = getResources();
211 if (props == null)
212 {
213 throw new InitializationException("LoggingService failed to " +
214 "get access to the properties for this service.");
215 }
216
217 // contains the logging configuration
218 Vector loggingConfig = new Vector(10);
219
220 //looking for default logger name
221 String defaultLoggerName = props.getString(LoggingConfig.DEFAULT);
222
223 //checking whether default logger is properly configured
224 if (defaultLoggerName == null)
225 {
226 throw new InitializationException("LoggingService can't find " +
227 "default logger name in the configuration file.");
228 }
229
230 // Get the list of facilities to configure
231 Vector facilities = props.getVector(LoggingConfig.FACILITIES);
232 for (Enumeration f = facilities.elements(); f.hasMoreElements();)
233 {
234 String facility = (String) f.nextElement();
235
236 LoggingConfig lc = getLoggingConfig();
237 lc.setName(facility);
238 lc.setInitResource(props);
239 lc.setServletContext(context);
240 lc.init();
241
242 loggingConfig.add((Object)lc);
243 }
244
245 //adds and configures loggers
246 for (Enumeration loggers = loggingConfig.elements();
247 loggers.hasMoreElements(); )
248 {
249 LoggingConfig lc = (LoggingConfig) loggers.nextElement();
250 loadLogger(lc);
251 }
252
253 defaultLogger = (Logger)loggersTable.get(defaultLoggerName);
254
255 //checking whether default logger is properly configured
256 if (defaultLogger == null)
257 {
258 throw new InitializationException("LoggingService can't find " +
259 "default logger in working loggers.");
260 }
261 }
262
263 /***
264 * Creates instances of the logger, configures it, and adds it to the
265 * hashTable. It skips loggers if there where errors.
266 *
267 * @param loggerDescription xml-Node defining the logger
268 */
269 protected void loadLogger(LoggingConfig loggingConfig)
270 throws InitializationException
271 {
272 String className = loggingConfig.getClassName();
273 String loggerName = loggingConfig.getName();
274
275 if (className == null || className.trim().equals(""))
276 {
277 throw new InitializationException("LoggingService can't find " +
278 "logger provider class name");
279 }
280
281 if (loggerName == null || loggerName.trim().equals(""))
282 {
283 throw new InitializationException("LoggingService can't find " +
284 "logger provider name");
285 }
286
287 if (loggersTable.containsKey(loggerName))
288 {
289 throw new InitializationException("LoggingService has found " +
290 "another logger of the name: " + loggerName);
291 }
292
293 Logger logger=null;
294 try
295 {
296 Class loggerClass= Class.forName(className);
297 logger=(Logger)loggerClass.newInstance();
298 }
299 catch (Exception e)
300 {
301 throw new InitializationException("LoggingService can't load " +
302 "logger provider: class doesn't implement Logger interface: " +
303 e.getMessage());
304 }
305
306 //inits logger
307 logger.init(loggingConfig);
308
309 // store it for later
310 loggersTable.put(loggerName, logger);
311 }
312
313 /***
314 * Shutdowns all loggers. After shutdown servlet logger is still available
315 * using the servlet log method
316 */
317 public void shutdown()
318 {
319 if (!getInit())
320 {
321 return;
322 }
323 Enumeration iter = loggersTable.elements();
324
325 while(iter.hasMoreElements())
326 {
327 ((Logger)iter.nextElement()).shutdown();
328 }
329
330 //HACK!!!!!
331 //some services may log using our services after shutdown
332 loggersTable.clear();
333 defaultLogger = simpleLogger;
334
335 //we don't set init as false, because we can still log.
336 }
337
338 /***
339 * This method returns default logger for Turbine System
340 */
341 public final Logger getLogger()
342 {
343 return defaultLogger;
344 }
345
346 /***
347 * This method returns logger with given name.
348 */
349 public Logger getLogger(String logName)
350 {
351 Logger logger = (Logger)loggersTable.get(logName);
352 if (logger == null)
353 {
354 return defaultLogger;
355 }
356 return logger;
357 }
358
359 /***
360 * This method sets the log level of the default logger.
361 */
362 public void setLogLevel(int level)
363 {
364 defaultLogger.setLogLevel(level);
365 }
366
367 /***
368 * This method sets the log level of the logger of given name.
369 */
370 public void setLogLevel(String logName, int level)
371 {
372 Logger logger = (Logger)loggersTable.get(logName);
373 if (logger != null)
374 {
375 logger.setLogLevel(level);
376 }
377 }
378
379 /***
380 * This method sets format style of the default logger
381 */
382 public void setFormat(String format)
383 {
384 defaultLogger.setFormat(format);
385 }
386
387 /***
388 * This method sets format style of the given logger.
389 */
390 public void setFormat(String logName, String format)
391 {
392 Logger logger = (Logger)loggersTable.get(logName);
393 if (logger != null)
394 {
395 logger.setFormat(format);
396 }
397 }
398
399 /***
400 * This is a log method with logLevel == DEBUG, printing is done by
401 * the default logger
402 */
403 public void debug(String message)
404 {
405 defaultLogger.debug(message);
406 }
407
408 /***
409 * This is a log method with logLevel == DEBUG, printing is done by
410 * the default logger
411 */
412 public void debug(String message, Throwable t)
413 {
414 defaultLogger.debug(message, t);
415 }
416
417 /***
418 * This is a log method with logLevel == DEBUG, printing is done by
419 * the given logger
420 */
421 public void debug(String logName, String message, Throwable t)
422 {
423 Logger logger = (Logger)loggersTable.get(logName);
424 if (logger != null)
425 {
426 logger.debug(message, t);
427 }
428 else
429 {
430 defaultLogger.debug("FROM logger:" + logName + ": " + message);
431 }
432 }
433
434 /***
435 * This is a log method with logLevel == DEBUG, printing is done by
436 * the given logger
437 */
438 public void debug(String logName, String message)
439 {
440 Logger logger = (Logger)loggersTable.get(logName);
441 if (logger != null)
442 {
443 logger.debug(message);
444 }
445 else
446 {
447 defaultLogger.debug("FROM logger:" + logName + ": " + message);
448 }
449 }
450
451 /***
452 * This is a log method with logLevel == DEBUG, printing is done by
453 * the default logger
454 */
455 public void debug(String message, RunData data)
456 {
457 defaultLogger.debug(message, data);
458 }
459
460 /***
461 * This is a log method with logLevel == DEBUG, printing is done by
462 * the default logger
463 */
464 public void debug(String message, RunData data, Throwable t)
465 {
466 defaultLogger.debug(message, data, t);
467 }
468
469 /***
470 * This is a log method with logLevel == DEBUG, printing is done by
471 * the given logger
472 */
473 public void debug(String logName, String message, RunData data, Throwable t)
474 {
475 Logger logger = (Logger)loggersTable.get(logName);
476 if (logger != null)
477 {
478 logger.debug(message, data, t);
479 }
480 else
481 {
482 defaultLogger.debug("FROM logger:" + logName + ": " + message);
483 }
484 }
485
486 /***
487 * This is a log method with logLevel == DEBUG, printing is done by
488 * the given logger
489 */
490 public void debug(String logName, String message, RunData data)
491 {
492 Logger logger = (Logger)loggersTable.get(logName);
493 if (logger != null)
494 {
495 logger.debug(message, data);
496 }
497 else
498 {
499 defaultLogger.debug("FROM logger:" + logName + ": " + message);
500 }
501 }
502
503 /***
504 * This is a log method with logLevel == INFO, printing is done by
505 * the default logger
506 */
507 public void info(String message)
508 {
509 defaultLogger.info(message);
510 }
511
512 /***
513 * This is a log method with logLevel == INFO, printing is done by
514 * the default logger
515 */
516 public void info(String message, Throwable t)
517 {
518 defaultLogger.info(message, t);
519 }
520
521 /***
522 * This is a log method with logLevel == INFO, printing is done by
523 * the given logger
524 */
525 public void info(String logName, String message)
526 {
527 Logger logger = (Logger)loggersTable.get(logName);
528 if (logger != null)
529 {
530 logger.info(message);
531 }
532 else
533 {
534 defaultLogger.info("FROM logger:" + logName + ": " + message);
535 }
536 }
537
538 /***
539 * This is a log method with logLevel == INFO, printing is done by
540 * the given logger
541 */
542 public void info(String logName, String message, Throwable t)
543 {
544 Logger logger = (Logger)loggersTable.get(logName);
545 if (logger != null)
546 {
547 logger.info(message, t);
548 }
549 else
550 {
551 defaultLogger.info("FROM logger:" + logName + ": " + message);
552 }
553 }
554
555 /***
556 * This is a log method with logLevel == INFO, printing is done by
557 * the default logger
558 */
559 public void info(String message, RunData data)
560 {
561 defaultLogger.info(message, data);
562 }
563
564 /***
565 * This is a log method with logLevel == INFO,printing is done by
566 * the default logger
567 */
568 public void info(String message, RunData data, Throwable t)
569 {
570 defaultLogger.info(message, data, t);
571 }
572
573 /***
574 * This is a log method with logLevel == INFO, printing is done by
575 * the given logger
576 */
577 public void info(String logName, String message, RunData data)
578 {
579 Logger logger = (Logger)loggersTable.get(logName);
580 if (logger != null)
581 {
582 logger.info(message, data);
583 }
584 else
585 {
586 defaultLogger.info("FROM logger:" + logName + ": " + message);
587 }
588 }
589
590 /***
591 * This is a log method with logLevel == INFO, printing is done by
592 * the given logger
593 */
594 public void info(String logName, String message, RunData data, Throwable t)
595 {
596 Logger logger = (Logger)loggersTable.get(logName);
597 if (logger != null)
598 {
599 logger.info(message, data, t);
600 }
601 else
602 {
603 defaultLogger.info("FROM logger:" + logName + ": " + message);
604 }
605 }
606
607 /***
608 * This is a log method with logLevel == WARN, printing is done by
609 * the default logger
610 */
611 public void warn(String message)
612 {
613 defaultLogger.warn(message);
614 }
615
616 /***
617 * This is a log method with logLevel == WARN, printing is done by
618 * the default logger
619 */
620 public void warn(String message, Throwable t)
621 {
622 defaultLogger.warn(message, t);
623 }
624
625 /***
626 * This is a log method with logLevel == WARN, printing is done by
627 * the given logger
628 */
629 public void warn(String logName, String message)
630 {
631 Logger logger = (Logger)loggersTable.get(logName);
632 if (logger != null)
633 {
634 logger.warn(message);
635 }
636 else
637 {
638 defaultLogger.warn("FROM logger:" + logName + ": " + message);
639 }
640 }
641
642 /***
643 * This is a log method with logLevel == WARN, printing is done by
644 * the given logger
645 */
646 public void warn(String logName, String message, Throwable t)
647 {
648 Logger logger = (Logger)loggersTable.get(logName);
649 if (logger != null)
650 {
651 logger.warn(message, t);
652 }
653 else
654 {
655 defaultLogger.warn("FROM logger:" + logName + ": " + message);
656 }
657 }
658
659 /***
660 * This is a log method with logLevel == WARN,printing is done by
661 * the default logger
662 */
663 public void warn(String message, RunData data)
664 {
665 defaultLogger.warn(message, data);
666 }
667
668 /***
669 * This is a log method with logLevel == WARN, printing is done by
670 * the default logger
671 */
672 public void warn(String message, RunData data, Throwable t)
673 {
674 defaultLogger.warn(message, data, t);
675 }
676
677 /***
678 * This is a log method with logLevel == WARN, printing is done by
679 * the given logger
680 */
681 public void warn(String logName, String message, RunData data)
682 {
683 Logger logger = (Logger)loggersTable.get(logName);
684 if (logger != null)
685 {
686 logger.warn(message, data);
687 }
688 else
689 {
690 defaultLogger.warn("FROM logger:" + logName + ": " + message);
691 }
692 }
693
694 /***
695 * This is a log method with logLevel == WARN, printing is done by
696 * the given logger
697 */
698 public void warn(String logName, String message, RunData data, Throwable t)
699 {
700 Logger logger = (Logger)loggersTable.get(logName);
701 if (logger != null)
702 {
703 logger.warn(message, data, t);
704 }
705 else
706 {
707 defaultLogger.warn("FROM logger:" + logName + ": " + message);
708 }
709 }
710
711 /***
712 * This is a log method with logLevel == ERROR, printing is done by
713 * the default logger
714 */
715 public void error(String message)
716 {
717 defaultLogger.error(message);
718 }
719
720 /***
721 * This is a log method with logLevel == ERROR, printing is done by
722 * the default logger
723 */
724 public void error(String message, Throwable t)
725 {
726 defaultLogger.error(message, t);
727 }
728
729 /***
730 * This is a log method with logLevel == ERROR, printing is done by
731 * the given logger
732 */
733 public void error(String logName, String message)
734 {
735 Logger logger = (Logger)loggersTable.get(logName);
736 if (logger != null)
737 {
738 logger.error(message);
739 }
740 else
741 {
742 defaultLogger.error("FROM logger:" + logName + ": " + message);
743 }
744 }
745
746 /***
747 * This is a log method with logLevel == ERROR, printing is done by
748 * the given logger
749 */
750 public void error(String logName, String message, Throwable t)
751 {
752 Logger logger = (Logger)loggersTable.get(logName);
753 if (logger != null)
754 {
755 logger.error(message, t);
756 }
757 else
758 {
759 defaultLogger.error("FROM logger:" + logName + ": " + message);
760 }
761 }
762
763 /***
764 * This is a log method with logLevel == ERROR, printing is done by
765 * the default logger
766 */
767 public void error(String message, RunData data)
768 {
769 defaultLogger.error(message, data);
770 }
771
772 /***
773 * This is a log method with logLevel == ERROR, printing is done by
774 * the default logger
775 */
776 public void error(String message, RunData data, Throwable t)
777 {
778 defaultLogger.error(message, data, t);
779 }
780
781 /***
782 * This is a log method with logLevel == ERROR, printing is done by
783 * the given logger
784 */
785 public void error(String logName, String message, RunData data)
786 {
787 Logger logger = (Logger)loggersTable.get(logName);
788 if (logger != null)
789 {
790 logger.error(message, data);
791 }
792 else
793 {
794 defaultLogger.error("FROM logger:" + logName + ": " + message);
795 }
796 }
797
798 /***
799 * This is a log method with logLevel == ERROR, printing is done by
800 * the given logger
801 */
802 public void error(String logName, String message, RunData data, Throwable t)
803 {
804 Logger logger = (Logger)loggersTable.get(logName);
805 if (logger != null)
806 {
807 logger.error(message, data, t);
808 }
809 else
810 {
811 defaultLogger.error("FROM logger:" + logName + ": " + message);
812 }
813 }
814 }
This page was automatically generated by Maven