1 8 package org.apache.avalon.excalibur.logger.test; 9 10 import java.io.InputStream ; 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 40 public class LogKitManagementTest 41 { 42 private static final String FORMAT = 44 "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}"; 45 46 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 [] args ) throws Exception 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 79 protected Logger getLogger() 80 { 81 return m_logger; 82 } 83 84 90 protected void initialize() 91 throws Exception 92 { 93 final String resourceName = this.getClass().getName().replace( '.', '/' ) + ".xtest"; 94 System.out.println("ResourceName = " + resourceName); 95 initialize( this.getClass().getClassLoader().getResource( resourceName ).openStream() ); 96 } 97 98 108 protected final void initialize( final InputStream testconf ) 109 throws Exception 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 128 final private void dispose() 129 { 130 } 131 132 135 final private Logger setupLogger() 136 throws Exception 137 { 138 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 158 final private Context setupContext( final Configuration conf ) 159 throws Exception 160 { 161 final DefaultContext context = new DefaultContext(); 164 return( context ); 165 } 166 } 167 | Popular Tags |