KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > DefaultLoggingManager


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.logging.logkit;
19
20 import org.apache.avalon.framework.logger.Logger;
21
22 import org.apache.avalon.logging.data.CategoryDirective;
23 import org.apache.avalon.logging.data.CategoriesDirective;
24
25 import org.apache.avalon.logging.provider.LoggingRuntimeException;
26 import org.apache.avalon.logging.provider.LoggingManager;
27 import org.apache.avalon.logging.provider.LoggingException;
28
29 import org.apache.avalon.util.i18n.ResourceManager;
30 import org.apache.avalon.util.i18n.Resources;
31
32 import org.apache.log.Hierarchy;
33 import org.apache.log.LogTarget;
34 import org.apache.log.Priority;
35 import org.apache.log.output.io.StreamTarget;
36 import org.apache.log.format.Formatter;
37
38 /**
39  * A <code>LoggerManager</code> interface declares operation supporting
40  * the management of a logging hierarchy.
41  *
42  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
43  */

44 public class DefaultLoggingManager implements LoggingManager
45 {
46     //---------------------------------------------------------------
47
// static
48
//---------------------------------------------------------------
49

50     private static final Resources REZ =
51       ResourceManager.getPackageResources( DefaultLoggingManager.class );
52
53     private static final String JavaDoc DEFAULT_PRIORITY = "INFO";
54
55     public static final String JavaDoc DEFAULT_FORMAT =
56        "[%7.7{priority}] (%{category}): %{message}\\n%{throwable}";
57
58     public static final Formatter FORMAT =
59        new StandardFormatter( DEFAULT_FORMAT );
60
61     public static final Formatter CONSOLE =
62        new StandardFormatter( DEFAULT_FORMAT, false );
63
64     //---------------------------------------------------------------
65
// immutable state
66
//---------------------------------------------------------------
67

68     /**
69      * The list of named logging targets.
70      */

71     private final LogTargetManager m_targets;
72
73     /**
74      * The implementation log hierarchy.
75      */

76     private final Hierarchy m_hierarchy = new Hierarchy();
77
78     /**
79      * Debug mode flag.
80      */

81     private final boolean m_debug;
82
83     //--------------------------------------------------------------
84
// mutable state
85
//--------------------------------------------------------------
86

87     /**
88      * The logging channel.
89      */

90     private Logger m_logger;
91
92     //--------------------------------------------------------------
93
// constructor
94
//--------------------------------------------------------------
95

96     /**
97      * Creation of a new logging manager using based on LogKit.
98      *
99      * @param logger boostrap logging channel
100      * @param targets the logging targets manager
101      * @param categories the initial logging categories
102      * @param channel the logging category to be used for internal log message
103      * @param debug a debug flag
104      */

105     public DefaultLoggingManager(
106       Logger logger, LogTargetManager targets, CategoriesDirective categories,
107       String JavaDoc channel, boolean debug )
108       throws Exception JavaDoc
109     {
110         if( null == targets )
111         {
112             throw new NullPointerException JavaDoc( "targets" );
113         }
114
115         m_debug = debug;
116         m_targets = targets;
117         m_logger = logger;
118
119         //
120
// setup the default logging priority
121
//
122

123         if( !debug )
124         {
125             String JavaDoc priority = getDefaultPriority( categories );
126             getHierarchy().setDefaultPriority(
127               Priority.getPriorityForName( priority ) );
128         }
129         else
130         {
131             getHierarchy().setDefaultPriority(
132               Priority.getPriorityForName( "DEBUG" ) );
133         }
134
135         LogTarget target = getDefaultLogTarget( categories );
136         getHierarchy().setDefaultLogTarget( target );
137
138         addCategories( categories );
139
140         m_logger = getLoggerForCategory( channel );
141         m_logger.debug( "logging system established" );
142     }
143
144     private LogTarget getDefaultLogTarget( CategoriesDirective categories )
145       throws LoggingException
146     {
147         String JavaDoc id = categories.getTarget();
148         if( null == id )
149         {
150             return new StreamTarget( System.out, FORMAT );
151         }
152         else
153         {
154             LogTarget target = m_targets.getLogTarget( id );
155             if( null != target ) return target;
156
157             final String JavaDoc error =
158               REZ.getString(
159                 "manager.invalid-default-target", id );
160             throw new LoggingException( error );
161         }
162     }
163
164     private String JavaDoc getDefaultPriority( CategoriesDirective categories )
165     {
166         if( null != categories )
167         {
168             String JavaDoc priority = categories.getPriority();
169             if( priority != null ) return priority;
170         }
171         return DEFAULT_PRIORITY;
172     }
173
174
175     //===============================================================
176
// LoggingManager
177
//===============================================================
178

179     /**
180      * Add a set of category entries using the supplied categories descriptor.
181      *
182      * @param descriptor a set of category descriptors to be added under the path
183      */

184     public void addCategories( CategoriesDirective descriptor )
185     {
186         addCategories( "", descriptor );
187     }
188
189     /**
190      * Add a set of category entries relative to the supplied base category
191      * path, using the supplied descriptor as the definition of subcategories.
192      *
193      * @param root the category base path
194      * @param directive a category directive to add
195      */

196     public void addCategories( String JavaDoc root, CategoriesDirective directive )
197     {
198         final String JavaDoc path = filter( root );
199         addCategory( path, directive.getPriority(), directive.getTarget() );
200         CategoryDirective[] categories = directive.getCategories();
201         for( int i = 0; i < categories.length; i++ )
202         {
203             CategoryDirective category = categories[i];
204             final String JavaDoc name = filter( category.getName() );
205             final String JavaDoc base = getBasePath( path, name );
206             if( category instanceof CategoriesDirective )
207             {
208                 CategoriesDirective c = (CategoriesDirective) category;
209                 addCategories( base, c );
210             }
211             else
212             {
213                 final String JavaDoc priority = category.getPriority();
214                 final String JavaDoc target = category.getTarget();
215                 addCategory( base, priority, target );
216             }
217         }
218     }
219
220     private String JavaDoc getBasePath( String JavaDoc root, String JavaDoc name )
221     {
222         if( root.equals( "" ) ) return name;
223         return filter( root + "." + name );
224     }
225
226     /**
227      * Return the Logger for the specified category.
228      * @param category the category path
229      * @return the logging channel
230      */

231     public Logger getLoggerForCategory( final String JavaDoc category )
232     {
233         org.apache.log.Logger log = addCategory( category, null, null );
234         return new LogKitLogger( log );
235     }
236
237     //===============================================================
238
// implementation
239
//===============================================================
240

241     private org.apache.log.Logger addCategory(
242       String JavaDoc path, String JavaDoc priority, String JavaDoc target )
243     {
244         return addCategory( path, priority, target, true );
245     }
246
247     private org.apache.log.Logger addCategory(
248       String JavaDoc path, String JavaDoc priority, String JavaDoc target, boolean notify )
249     {
250         final String JavaDoc name = filter( path );
251         final org.apache.log.Logger logger;
252
253         if( null != priority )
254         {
255             if( !name.equals( "" ) )
256             {
257                 final String JavaDoc message =
258                   REZ.getString(
259                     "manager.notify.add-category-name-priority", name, priority.toLowerCase() );
260                 debug( message );
261             }
262             else
263             {
264                 final String JavaDoc message =
265                   REZ.getString(
266                     "manager.notify.add-category-priority", priority.toLowerCase() );
267                 debug( message );
268             }
269         }
270         else
271         {
272             if( !name.equals( "" ) )
273             {
274                 final String JavaDoc message =
275                   REZ.getString(
276                     "manager.notify.add-category-name", name );
277                 debug( message );
278             }
279             else
280             {
281                 final String JavaDoc message =
282                   REZ.getString(
283                     "manager.notify.add-category" );
284                 debug( message );
285             }
286         }
287
288         try
289         {
290             logger = getHierarchy().getLoggerFor( name );
291         }
292         catch( Throwable JavaDoc e )
293         {
294             final String JavaDoc error =
295               REZ.getString(
296                 "manager.error.internal", name );
297             throw new LoggingRuntimeException( error, e );
298         }
299
300         if( !m_debug && priority != null )
301         {
302             final Priority priorityValue = Priority.getPriorityForName( priority );
303             if( !priorityValue.getName().equals( priority ) )
304             {
305                 final String JavaDoc message =
306                   REZ.getString( "manager.error.priority", priority, name );
307                 throw new IllegalArgumentException JavaDoc( message );
308             }
309             logger.setPriority( priorityValue );
310         }
311
312         if( target != null )
313         {
314             if( !target.equals( "default" ) )
315             {
316                 final LogTarget logTarget =
317                   (LogTarget) m_targets.getLogTarget( target );
318                 if( logTarget != null )
319                 {
320                     logger.setLogTargets( new LogTarget[]{ logTarget } );
321                 }
322             }
323         }
324
325         return logger;
326     }
327
328     private String JavaDoc filter( String JavaDoc name )
329     {
330         if( name == null ) return "";
331         String JavaDoc path = name.replace( '/', '.' );
332         if( path.startsWith( "." ) )
333         {
334             path = path.substring( 1 );
335             return filter( path );
336         }
337         if( path.endsWith( "." ) )
338         {
339             path = path.substring( 0, path.length() -1 );
340             return filter( path );
341         }
342         return path;
343     }
344
345     private Hierarchy getHierarchy()
346     {
347         return m_hierarchy;
348     }
349
350     private void debug( String JavaDoc message )
351     {
352         if( m_logger != null ) m_logger.debug( message );
353     }
354 }
355
Popular Tags