KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > Log4JLoggerManager


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.logger;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12 import org.apache.avalon.framework.logger.Log4JLogger;
13 import org.apache.avalon.framework.logger.Logger;
14 import org.apache.log4j.Category;
15 import org.apache.log4j.Hierarchy;
16
17 /**
18  * Log4JLoggerManager implementation. This is the interface used to get instances of
19  * a Logger for your system. This manager does not set up the categories--it
20  * leaves that as an excercise for Log4J's construction.
21  *
22  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
23  * @version CVS $Revision: 1.1 $ $Date: 2002/01/17 20:44:59 $
24  * @since 4.1
25  */

26 public class Log4JLoggerManager
27     implements LoggerManager
28 {
29     /** Map for name to logger mapping */
30     final private Map JavaDoc m_loggers = new HashMap JavaDoc();
31
32     /** The root logger to configure */
33     private String JavaDoc m_prefix;
34
35     /** The hierarchy private to LogKitManager */
36     private Hierarchy m_hierarchy;
37
38     /** The default logger used for this system */
39     final private Logger m_defaultLogger;
40
41     /**
42      * Creates a new <code>DefaultLogKitManager</code>. It will use a new <code>Hierarchy</code>.
43      */

44     public Log4JLoggerManager()
45     {
46         this( Category.getDefaultHierarchy() );
47     }
48
49     /**
50      * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code>.
51      */

52     public Log4JLoggerManager( final Hierarchy hierarchy )
53     {
54         this( null, hierarchy );
55     }
56
57     /**
58      * Creates a new <code>DefaultLogKitManager</code> using
59      * specified logger name as root logger.
60      */

61     public Log4JLoggerManager( final String JavaDoc prefix )
62     {
63         this( prefix, Category.getDefaultHierarchy() );
64     }
65
66     /**
67      * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code> using
68      * specified logger name as root logger.
69      */

70     public Log4JLoggerManager( final String JavaDoc prefix, final Hierarchy hierarchy )
71     {
72         this( prefix, hierarchy,
73               new Log4JLogger( hierarchy.getInstance("") ) );
74     }
75
76     /**
77      * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code> using
78      * specified logger name as root logger.
79      */

80     public Log4JLoggerManager( final String JavaDoc prefix, final Hierarchy hierarchy, final Logger defaultLogger )
81     {
82         m_prefix = prefix;
83         m_hierarchy = hierarchy;
84         m_defaultLogger = defaultLogger;
85     }
86
87     /**
88      * Retrieves a Logger from a category name. Usually
89      * the category name refers to a configuration attribute name. If
90      * this LogKitManager does not have the match the default Logger will
91      * be returned and a warning is issued.
92      *
93      * @param categoryName The category name of a configured Logger.
94      * @return the Logger.
95      */

96     public final Logger getLoggerForCategory( final String JavaDoc categoryName )
97     {
98         Logger logger = (Logger) m_loggers.get( categoryName );
99
100         if( null != logger )
101         {
102             if( m_defaultLogger.isDebugEnabled() )
103             {
104                 m_defaultLogger.debug( "Logger for category " + categoryName + " returned" );
105             }
106             return logger;
107         }
108
109         if( m_defaultLogger.isDebugEnabled() )
110         {
111             m_defaultLogger.debug( "Logger for category " + categoryName
112                               + " not defined in configuration. New Logger created and returned" );
113         }
114
115         logger = new Log4JLogger( m_hierarchy.getInstance( categoryName ) );
116         m_loggers.put( categoryName, logger );
117         return logger;
118     }
119
120     public final Logger getDefaultLogger()
121     {
122         return m_defaultLogger;
123     }
124 }
125
Popular Tags