View Javadoc

1   package org.apache.jetspeed.services.logging;
2   
3   /*
4    * Copyright 2001,2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  // Java classes
20  import java.util.Iterator;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  // Servlet API
25  import javax.servlet.ServletConfig;
26  import javax.servlet.ServletContext;
27  
28  // Turbine classes
29  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  /***
41   * This service now only functions as an interim implementation of
42   * Turbine's LoggingService. In order to facilitate the new Log4J logging
43   * in Jetspeed, the default logging service behavior of Turbine must be overridden.
44   * The JetspeedLoggingService now just reroutes to JetspeedLogFactoryService
45   *
46   *
47   * @see org.apache.jetspeed.services.logging.JetspeedLogFactoryService
48   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
49   * @author <a href="mailto:harald@ommang.com">Harald Ommang</a>
50   * @version $Id: JetspeedLoggingService.java,v 1.4 2004/02/23 03:33:29 jford Exp $
51   */
52  public class JetspeedLoggingService
53  extends TurbineBaseService
54  implements LoggingService
55  {
56      /***
57       * Static initialization. Facilitates configuration via JetspeedLogFactoryService
58       */    
59      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedLoggingService.class.getName());
60      
61      /***
62       * loggers repository
63       */
64      private HashMap loggers;
65  
66      /***
67       * logger for methods without target
68       */
69      private Logger defaultLogger;
70  
71      /***
72       * bootstrap and shutdown logger using context.log
73       */
74      private Logger simpleLogger;
75  
76      /***
77       * context for resolving paths and servlet logging
78       */
79      private ServletContext context = null;
80  
81      /***
82       * Resources for this Service
83       */
84      private ResourceService resources = null;
85  
86      public JetspeedLoggingService()
87      {
88          loggers = new HashMap();
89          defaultLogger = null;
90      }
91  
92      /***
93       * Load all configured components and initialize them. This is
94       * a zero parameter variant which queries the Turbine Servlet
95       * for its config.
96       *
97       * @throws InitializationException Something went wrong in the init
98       *         stage
99       */ 
100     public void init()
101     throws InitializationException
102     {
103         ServletConfig conf = Turbine.getTurbineServletConfig();
104         init(conf);
105     }
106 
107     /***
108      * Inits the service using servlet parameters to obtain path to the
109      * configuration file. Change relatives paths.
110      *
111      * @param config The ServletConfiguration from Turbine
112      *
113      * @throws InitializationException Something went wrong when starting up.
114      */
115     public void init(ServletConfig config) 
116     throws InitializationException
117     {
118         context = config.getServletContext();
119 
120         // Create bootstrap logger, for handling exceptions during service
121         // initialization.
122         defaultLogger = new BaseLogger(logger);
123 
124         simpleLogger = defaultLogger;
125 
126         internalInit();
127         setInit(true);
128     }
129 
130     /***
131      * This gets the ResourceService associated to this Service
132      */
133     public ResourceService getResources()
134     {
135         if (resources == null)
136         {
137             // Get the properties for this Service
138             resources = TurbineResources
139                         .getResources(TurbineServices.SERVICE_PREFIX 
140                                       + LoggingService.SERVICE_NAME);
141 
142             //add webappRoot manually - cos logging is a primary
143             //service and so it is not yet defined
144             String webappRoot = context.getRealPath("/");
145             resources.setProperty(Turbine.WEBAPP_ROOT, webappRoot);
146         }
147         return (resources);
148     }
149 
150     /***
151      * This method initializes the service.
152      */
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         //looking for default logger name
163         String defaultLoggerName = props.getString(LoggingConfig.DEFAULT);
164 
165         //checking whether default logger is properly configured
166         if (defaultLoggerName == null)
167         {
168             throw new InitializationException("LoggingService can't find " 
169                                               + "default logger name in the configuration file.");
170         }
171 
172         // Create default logger
173         loggers.put(defaultLoggerName, defaultLogger);
174 
175         //checking whether default logger is properly configured
176         if (defaultLogger == null)
177         {
178             throw new InitializationException("LoggingService can't find " 
179                                               + "default logger in working loggers.");
180         }
181     }
182 
183     /***
184      * Shutdowns all loggers. After shutdown servlet logger is still available
185      * using the servlet log method
186      */
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         //we don't set init as false, because we can still log.
202     }
203 
204     /***
205      * This method returns default logger for Turbine System
206      */
207     public final Logger getLogger()
208     {
209         return defaultLogger;
210     }
211 
212     /***
213      * This method returns logger with given name.
214      */
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     /***
234      * This method sets the log level of the default logger.
235      */
236     public void setLogLevel(int level)
237     {
238         defaultLogger.setLogLevel(level);
239     }
240 
241     /***
242      * This method sets the log level of the logger of given name.
243      */
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     /***
254      * This method sets format style of the default logger
255      */
256     public void setFormat(String format)
257     {
258         defaultLogger.setFormat(format);
259     }
260 
261     /***
262      * This method sets format style of the given logger.
263      */
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     /***
274      * This is a log method with logLevel == DEBUG, printing is done by
275      * the default logger
276      */
277     public void debug(String message)
278     {
279         defaultLogger.debug(message);
280     }
281 
282     /***
283      * This is a log method with logLevel == DEBUG, printing is done by
284      * the default logger
285      */
286     public void debug(String message, Throwable t)
287     {
288         defaultLogger.debug(message, t);
289     }
290 
291     /***
292      * This is a log method with logLevel == DEBUG, printing is done by
293      * the given logger
294      */
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     /***
309      * This is a log method with logLevel == DEBUG, printing is done by
310      * the given logger
311      */
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     /***
326      * This is a log method with logLevel == DEBUG, printing is done by
327      * the default logger
328      */
329     public void debug(String message, RunData data)
330     {
331         defaultLogger.debug(message);
332     }
333 
334     /***
335      * This is a log method with logLevel == DEBUG, printing is done by
336      * the default logger
337      */
338     public void debug(String message, RunData data, Throwable t)
339     {
340         defaultLogger.debug(message, t);
341     }
342 
343     /***
344      * This is a log method with logLevel == DEBUG, printing is done by
345      * the given logger
346      */
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     /***
361      * This is a log method with logLevel == DEBUG, printing is done by
362      * the given logger
363      */
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     /***
378      * This is a log method with logLevel == INFO, printing is done by
379      * the default logger
380      */
381     public void info(String message)
382     {
383         defaultLogger.info(message);
384     }
385 
386     /***
387      * This is a log method with logLevel == INFO, printing is done by
388      * the default logger
389      */
390     public void info(String message, Throwable t)
391     {
392         defaultLogger.info(message, t);
393     }
394 
395     /***
396      * This is a log method with logLevel == INFO, printing is done by
397      * the given logger
398      */
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     /***
413      * This is a log method with logLevel == INFO, printing is done by
414      * the given logger
415      */
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     /***
430      * This is a log method with logLevel == INFO, printing is done by
431      * the default logger
432      */
433     public void info(String message, RunData data)
434     {
435         defaultLogger.info(message);
436     }
437 
438     /***
439      * This is a log method with logLevel == INFO,printing is done by
440      * the default logger
441      */
442     public void info(String message, RunData data, Throwable t)
443     {
444         defaultLogger.info(message, t);
445     }
446 
447     /***
448      * This is a log method with logLevel == INFO, printing is done by
449      * the given logger
450      */
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     /***
465      * This is a log method with logLevel == INFO, printing is done by
466      * the given logger
467      */
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     /***
482      * This is a log method with logLevel == WARN, printing is done by
483      * the default logger
484      */
485     public void warn(String message)
486     {
487         defaultLogger.warn(message);
488     }
489 
490     /***
491      * This is a log method with logLevel == WARN, printing is done by
492      * the default logger
493      */
494     public void warn(String message, Throwable t)
495     {
496         defaultLogger.warn(message, t);
497     }
498 
499     /***
500      * This is a log method with logLevel == WARN, printing is done by
501      * the given logger
502      */
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     /***
517      * This is a log method with logLevel == WARN, printing is done by
518      * the given logger
519      */
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     /***
534      * This is a log method with logLevel == WARN,printing is done by
535      * the default logger
536      */
537     public void warn(String message, RunData data)
538     {
539         defaultLogger.warn(message);
540     }
541 
542     /***
543      * This is a log method with logLevel == WARN, printing is done by
544      * the default logger
545      */
546     public void warn(String message, RunData data, Throwable t)
547     {
548         defaultLogger.warn(message, t);
549     }
550 
551     /***
552      * This is a log method with logLevel == WARN, printing is done by
553      * the given logger
554      */
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     /***
569      * This is a log method with logLevel == WARN, printing is done by
570      * the given logger
571      */
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     /***
586      * This is a log method with logLevel == ERROR, printing is done by
587      * the default logger
588      */
589     public void error(String message)
590     {
591         defaultLogger.error(message);
592     }
593 
594     /***
595      * This is a log method with logLevel == ERROR, printing is done by
596      * the default logger
597      */
598     public void error(String message, Throwable t)
599     {
600         defaultLogger.error(message, t);
601     }
602 
603     /***
604      * This is a log method with logLevel == ERROR, printing is done by
605      * the given logger
606      */
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     /***
621      * This is a log method with logLevel == ERROR, printing is done by
622      * the given logger
623      */
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     /***
638      * This is a log method with logLevel == ERROR, printing is done by
639      * the default logger
640      */
641     public void error(String message, RunData data)
642     {
643         defaultLogger.error(message);
644     }
645 
646     /***
647      * This is a log method with logLevel == ERROR, printing is done by
648      * the default logger
649      */
650     public void error(String message, RunData data, Throwable t)
651     {
652         defaultLogger.error(message, t);
653     }
654 
655     /***
656      * This is a log method with logLevel == ERROR, printing is done by
657      * the given logger
658      */
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     /***
673      * This is a log method with logLevel == ERROR, printing is done by
674      * the given logger
675      */
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 }