KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > test > LogKitManagementTest


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.test;
9
10 import java.io.InputStream JavaDoc;
11
12 import org.apache.avalon.framework.component.ComponentManager;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.configuration.ConfigurationException;
15 import org.apache.avalon.framework.configuration.DefaultConfiguration;
16 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
17 import org.apache.avalon.framework.context.Context;
18 import org.apache.avalon.framework.context.ContextException;
19 import org.apache.avalon.framework.context.DefaultContext;
20 import org.apache.avalon.framework.logger.LogKitLogger;
21
22 import org.apache.avalon.excalibur.component.DefaultRoleManager;
23 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
24 import org.apache.avalon.excalibur.logger.DefaultLogKitManager;
25 import org.apache.avalon.excalibur.logger.LogKitManager;
26
27 import org.apache.log.Hierarchy;
28 import org.apache.log.Logger;
29 import org.apache.log.LogTarget;
30 import org.apache.log.Priority;
31 import org.apache.log.format.PatternFormatter;
32 import org.apache.log.output.io.StreamTarget;
33
34 /**
35  * LogKitManagementTest.
36  *
37  * @author <a HREF="mailto:giacomo@apache,org">Giacomo Pati</a>
38  * @version CVS $Revision: 1.5 $ $Date: 2001/12/11 09:53:39 $
39  */

40 public class LogKitManagementTest
41 {
42     ///Format of default formatter
43
private static final String JavaDoc FORMAT =
44         "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}";
45
46     //The default logger
47
private Logger m_logger;
48     private ExcaliburComponentManager m_manager;
49
50     protected Priority m_logPriority = Priority.DEBUG;
51     protected LogKitManager m_lkm;
52
53     public static void main( String JavaDoc [] args ) throws Exception JavaDoc
54     {
55         LogKitManagementTest lkmt = new LogKitManagementTest();
56         lkmt.initialize();
57         lkmt.test();
58         lkmt.dispose();
59     }
60
61     protected void test()
62     {
63         Logger log1 = m_lkm.getLogger( "cocoon" );
64         log1.debug( "this is the cocoon logger" );
65         log1.info( "this is the cocoon logger" );
66         log1.info( "this is the cocoon logger" );
67         Logger log2 = log1.getChildLogger( "classloader" );
68         log2.debug( "this is the childlogger classloader of the cocoon logger" );
69         log2.info( "this is the childlogger classloader of the cocoon logger" );
70         Logger log3 = m_lkm.getLogger( "cocoon.classloader" );
71         log3.debug( "this is the cocoon.classloader logger" );
72         log3.info( "this is the cocoon.classloader logger" );
73         Logger log4 = m_lkm.getLogger( "foo" );
74         log4.debug( "this is the foo logger" );
75         log4.info( "this is the foo logger" );
76     }
77
78     /** Return the logger */
79     protected Logger getLogger()
80     {
81         return m_logger;
82     }
83
84     /**
85      * Initializes the ComponentManager
86      *
87      * The configuration file is determined by the class name plus .xtest appended,
88      * all '.' replaced by '/' and loaded as a resource via classpath
89      */

90     protected void initialize()
91         throws Exception JavaDoc
92     {
93         final String JavaDoc resourceName = this.getClass().getName().replace( '.', '/' ) + ".xtest";
94         System.out.println("ResourceName = " + resourceName);
95         initialize( this.getClass().getClassLoader().getResource( resourceName ).openStream() );
96     }
97
98     /**
99      * Initializes the ComponentManager
100      *
101      * @param testconf The configuration file is passed as a <code>InputStream</code> *
102      * A common way to supply a InputStream is to overwrite the initialize() method
103      * in the sub class, do there whatever is needed to get the right InputStream object
104      * supplying a conformant xtest configuartion and pass it to this initialize method.
105      * the mentioned initialize method is also the place to set a different logging priority
106      * to the member variable m_logPriority.
107      */

108     protected final void initialize( final InputStream JavaDoc testconf )
109         throws Exception JavaDoc
110     {
111         m_logger = setupLogger();
112         m_logger.debug( "LogKitManagementTest.initialize" );
113
114         final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
115         final Configuration conf = builder.build( testconf );
116         final Context context = setupContext( conf.getChild( "context" ) );
117
118         final DefaultLogKitManager lkm = new DefaultLogKitManager();
119         lkm.enableLogging( new LogKitLogger( m_logger ) );
120         lkm.contextualize( context );
121         lkm.configure( conf );
122         m_lkm = lkm;
123     }
124
125     /**
126      * Disposes the <code>ComponentManager</code>
127      */

128     final private void dispose()
129     {
130     }
131
132     /**
133      * Set up logger configuration
134      */

135     final private Logger setupLogger()
136         throws Exception JavaDoc
137     {
138         //FIXME(GP): This method should setup a LogConfigurator and LogManager
139
// according to the configuration spec. not yet completed/implemented
140
// It will return a default logger for now.
141
final Logger logger = Hierarchy.getDefaultHierarchy().getLoggerFor( "" );
142         logger.setPriority( m_logPriority );
143
144         final PatternFormatter formatter = new PatternFormatter( FORMAT );
145         final StreamTarget target = new StreamTarget( System.out, formatter );
146         logger.setLogTargets( new LogTarget[] { target } );
147
148         return logger;
149     }
150
151     /**
152      * set up a context according to the xtest configuration specifications context
153      * element.
154      *
155      * A method addContext(DefaultContext context) is called here to enable subclasses
156      * to put additional objects into the context programmatically.
157      */

158     final private Context setupContext( final Configuration conf )
159         throws Exception JavaDoc
160     {
161         //FIXME(GP): This method should setup the Context object according to the
162
// configuration spec. not yet completed
163
final DefaultContext context = new DefaultContext();
164         return( context );
165     }
166 }
167
Popular Tags