KickJava   Java API By Example, From Geeks To Geeks.

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


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.configuration.Configurable;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.configuration.ConfigurationException;
15 import org.apache.avalon.framework.context.Context;
16 import org.apache.avalon.framework.context.ContextException;
17 import org.apache.avalon.framework.context.Contextualizable;
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.logger.LogEnabled;
20 import org.apache.avalon.framework.logger.LogKitLogger;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.log.Hierarchy;
23 import org.apache.log.LogTarget;
24 import org.apache.log.Priority;
25
26 /**
27  * LogKitLoggerManager implementation. It populates the LoggerManager
28  * from a configuration file.
29  *
30  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
31  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
32  * @version CVS $Revision: 1.1 $ $Date: 2002/01/17 20:44:59 $
33  * @since 4.0
34  */

35 public class LogKitLoggerManager
36     implements LoggerManager, Contextualizable, Configurable
37 {
38     /** Map for name to logger mapping */
39     final private Map JavaDoc m_loggers = new HashMap JavaDoc();
40
41     /** The context object */
42     private Context m_context;
43
44     /** The hierarchy private to LogKitManager */
45     private Hierarchy m_hierarchy;
46
47     /** The root logger to configure */
48     private String JavaDoc m_prefix;
49
50     /** The default logger used for this system */
51     final private Logger m_defaultLogger;
52
53     /**
54      * Creates a new <code>DefaultLogKitManager</code>. It will use a new <code>Hierarchy</code>.
55      */

56     public LogKitLoggerManager()
57     {
58         this( new Hierarchy() );
59     }
60
61     /**
62      * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code>.
63      */

64     public LogKitLoggerManager( final Hierarchy hierarchy )
65     {
66         this( null, hierarchy );
67     }
68
69     /**
70      * Creates a new <code>DefaultLogKitManager</code> using
71      * specified logger name as root logger.
72      */

73     public LogKitLoggerManager( final String JavaDoc prefix )
74     {
75         this( prefix, new Hierarchy() );
76     }
77
78     /**
79      * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code> using
80      * specified logger name as root logger.
81      */

82     public LogKitLoggerManager( final String JavaDoc prefix, final Hierarchy hierarchy )
83     {
84         this( prefix, hierarchy,
85               new LogKitLogger( hierarchy.getLoggerFor("") ) );
86     }
87
88     /**
89      * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code> using
90      * specified logger name as root logger.
91      */

92     public LogKitLoggerManager( final String JavaDoc prefix, final Hierarchy hierarchy, final Logger defaultLogger )
93     {
94         m_prefix = prefix;
95         m_hierarchy = hierarchy;
96         m_defaultLogger = defaultLogger;
97     }
98
99     /**
100      * Retrieves a Logger from a category name. Usually
101      * the category name refers to a configuration attribute name. If
102      * this LogKitManager does not have the match the default Logger will
103      * be returned and a warning is issued.
104      *
105      * @param categoryName The category name of a configured Logger.
106      * @return the Logger.
107      */

108     public final Logger getLoggerForCategory( final String JavaDoc categoryName )
109     {
110         final Logger logger = (Logger) m_loggers.get( categoryName );
111
112         if( null != logger )
113         {
114             if( m_defaultLogger.isDebugEnabled() )
115             {
116                 m_defaultLogger.debug( "Logger for category " + categoryName + " returned" );
117             }
118             return logger;
119         }
120
121         if( m_defaultLogger.isDebugEnabled() )
122         {
123             m_defaultLogger.debug( "Logger for category " + categoryName
124                               + " not defined in configuration. New Logger created and returned" );
125         }
126
127         return new LogKitLogger(m_hierarchy.getLoggerFor( categoryName ));
128     }
129
130     public final Logger getDefaultLogger()
131     {
132         return m_defaultLogger;
133     }
134
135     /**
136      * Reads a context object.
137      *
138      * @param context The context object.
139      * @throws ContextException if the context is malformed
140      */

141     public final void contextualize( final Context context )
142         throws ContextException
143     {
144         m_context = context;
145     }
146
147     /**
148      * Reads a configuration object and creates the category mapping.
149      *
150      * @param configuration The configuration object.
151      * @throws ConfigurationException if the configuration is malformed
152      */

153     public final void configure( final Configuration configuration )
154         throws ConfigurationException
155     {
156         final Configuration factories = configuration.getChild( "factories" );
157         final LogTargetFactoryManager targetFactoryManager = setupTargetFactoryManager( factories );
158
159         final Configuration targets = configuration.getChild( "targets" );
160         final LogTargetManager targetManager = setupTargetManager( targets, targetFactoryManager );
161
162         final Configuration categories = configuration.getChild( "categories" );
163         final Configuration [] category = categories.getChildren( "category" );
164         setupLoggers( targetManager, m_prefix, category );
165     }
166
167     /**
168      * Setup a LogTargetFactoryManager
169      *
170      * @param configuration The configuration object.
171      * @throws ConfigurationException if the configuration is malformed
172      */

173     private final LogTargetFactoryManager setupTargetFactoryManager( final Configuration configuration )
174         throws ConfigurationException
175     {
176         final DefaultLogTargetFactoryManager targetFactoryManager = new DefaultLogTargetFactoryManager();
177         if( targetFactoryManager instanceof LogEnabled )
178         {
179             targetFactoryManager.enableLogging( m_defaultLogger );
180         }
181
182         if( targetFactoryManager instanceof Contextualizable )
183         {
184             try
185             {
186                 targetFactoryManager.contextualize( m_context );
187             }
188             catch( final ContextException ce )
189             {
190                 throw new ConfigurationException( "cannot contextualize default factory manager", ce );
191             }
192         }
193
194         targetFactoryManager.configure( configuration );
195
196         return targetFactoryManager;
197     }
198
199     /**
200      * Setup a LogTargetManager
201      *
202      * @param configuration The configuration object.
203      * @throws ConfigurationException if the configuration is malformed
204      */

205     private final LogTargetManager setupTargetManager( final Configuration configuration,
206                                                        final LogTargetFactoryManager targetFactoryManager )
207         throws ConfigurationException
208     {
209         final DefaultLogTargetManager targetManager = new DefaultLogTargetManager();
210
211         if (targetManager instanceof LogEnabled)
212         {
213             targetManager.enableLogging( m_defaultLogger );
214         }
215
216         if( targetManager instanceof Contextualizable )
217         {
218             try
219             {
220                 targetManager.contextualize( m_context );
221             }
222             catch( final ContextException ce )
223             {
224                 throw new ConfigurationException( "cannot contextualize factory manager", ce );
225             }
226         }
227
228         if( targetManager instanceof LogTargetFactoryManageable )
229         {
230             targetManager.setLogTargetFactoryManager( targetFactoryManager );
231         }
232
233         if( targetManager instanceof Configurable )
234         {
235             targetManager.configure( configuration );
236         }
237
238         return targetManager;
239     }
240
241     /**
242      * Setup Loggers
243      *
244      * @param configuration [] The array object of configurations for categories.
245      * @throws ConfigurationException if the configuration is malformed
246      */

247     private final void setupLoggers( final LogTargetManager targetManager,
248                                      final String JavaDoc parentCategory,
249                                      final Configuration [] categories )
250         throws ConfigurationException
251     {
252         for( int i = 0; i < categories.length; i++ )
253         {
254             final String JavaDoc category = categories[i].getAttribute( "name" );
255             final String JavaDoc loglevel = categories[i].getAttribute( "log-level" ).toUpperCase();
256
257             final Configuration [] targets = categories[i].getChildren( "log-target" );
258             final LogTarget [] log_targets = new LogTarget[ targets.length ];
259             for( int j = 0; j < targets.length; j++ )
260             {
261                 final String JavaDoc id = targets[j].getAttribute( "id-ref" );
262                 log_targets[j] = targetManager.getLogTarget( id );
263             }
264
265             if( "".equals( category ) && log_targets.length > 0 )
266             {
267                 m_hierarchy.setDefaultPriority( Priority.getPriorityForName( loglevel ) );
268                 m_hierarchy.setDefaultLogTargets( log_targets );
269             }
270
271             final String JavaDoc full_category;
272             if( null == parentCategory )
273             {
274                 full_category = category;
275             }
276             else
277             {
278                 full_category = parentCategory + org.apache.log.Logger.CATEGORY_SEPARATOR + category;
279             }
280
281             final org.apache.log.Logger logger = m_hierarchy.getLoggerFor( full_category );
282             m_loggers.put( full_category, new LogKitLogger( logger ) );
283             if( m_defaultLogger.isDebugEnabled() )
284             {
285                 m_defaultLogger.debug( "added logger for category " + full_category );
286             }
287             logger.setPriority( Priority.getPriorityForName( loglevel ) );
288             logger.setLogTargets( log_targets );
289
290             final Configuration [] sub_categories = categories[i].getChildren( "category" );
291             if( null != sub_categories )
292             {
293                 setupLoggers( targetManager, full_category, sub_categories );
294             }
295         }
296     }
297 }
298
Popular Tags