KickJava   Java API By Example, From Geeks To Geeks.

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


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.Loggable;
22 import org.apache.log.Hierarchy;
23 import org.apache.log.LogTarget;
24 import org.apache.log.Logger;
25 import org.apache.log.Priority;
26
27 /**
28  * Default LogManager implementation. It populates the LogManager
29  * from a configuration file.
30  *
31  * @deprecated we should use the new LogKitLoggerManager interface that directly
32  * supports the new framework Logger interface.
33  *
34  * @author <a HREF="mailto:giacomo@apache,org">Giacomo Pati</a>
35  * @version CVS $Revision: 1.13 $ $Date: 2002/01/17 20:44:59 $
36  * @since 4.0
37  */

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

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

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

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

83     public DefaultLogKitManager( final String JavaDoc prefix, final Hierarchy hierarchy )
84     {
85         m_prefix = prefix;
86         m_hierarchy = hierarchy;
87     }
88
89     public void setLogger( final Logger logger )
90     {
91         enableLogging( new LogKitLogger( logger ) );
92     }
93
94     /**
95      * Retrieves a Logger from a category name. Usually
96      * the category name refers to a configuration attribute name. If
97      * this LogKitManager does not have the match the default Logger will
98      * be returned and a warning is issued.
99      *
100      * @param categoryName The category name of a configured Logger.
101      * @return the Logger.
102      */

103     public final Logger getLogger( final String JavaDoc categoryName )
104     {
105         final Logger logger = (Logger) m_loggers.get( categoryName );
106
107         if( null != logger )
108         {
109             if( getLogger().isDebugEnabled() )
110             {
111                 getLogger().debug( "Logger for category " + categoryName + " returned" );
112             }
113             return logger;
114         }
115
116         if( getLogger().isDebugEnabled() )
117         {
118             getLogger().debug( "Logger for category " + categoryName
119                               + " not defined in configuration. New Logger created and returned" );
120         }
121
122         return m_hierarchy.getLoggerFor( categoryName );
123     }
124
125     /**
126      * Retrieve Hierarchy for Loggers configured by the system.
127      *
128      * @return the Hierarchy
129      */

130     public Hierarchy getHierarchy()
131     {
132         return m_hierarchy;
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( getLogger() );
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( getLogger() );
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 + Logger.CATEGORY_SEPARATOR + category;
279             }
280
281             final Logger logger = m_hierarchy.getLoggerFor( full_category );
282             m_loggers.put( full_category, logger );
283             if( getLogger().isDebugEnabled() )
284             {
285                 getLogger().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