1 8 package org.apache.avalon.excalibur.logger.factory; 9 10 import java.io.OutputStream ; 11 import org.apache.avalon.excalibur.logger.factory.AbstractTargetFactory; 12 import org.apache.avalon.framework.configuration.Configuration; 13 import org.apache.avalon.framework.configuration.ConfigurationException; 14 import org.apache.avalon.framework.context.Context; 15 import org.apache.avalon.framework.context.ContextException; 16 import org.apache.avalon.framework.context.Contextualizable; 17 import org.apache.avalon.framework.context.DefaultContext; 18 import org.apache.log.LogTarget; 19 import org.apache.log.format.ExtendedPatternFormatter; 20 import org.apache.log.format.Formatter; 21 import org.apache.log.format.PatternFormatter; 22 import org.apache.log.format.RawFormatter; 23 import org.apache.log.output.io.StreamTarget; 24 25 48 public class StreamTargetFactory 49 extends AbstractTargetFactory 50 implements Contextualizable 51 { 52 53 56 public LogTarget createTarget( final Configuration configuration ) 57 throws ConfigurationException 58 { 59 OutputStream stream; 60 61 final Configuration streamConfig = configuration.getChild( "stream", false ); 62 if ( null == streamConfig ) 63 { 64 stream = System.err; 65 } 66 else 67 { 68 final String streamName = streamConfig.getValue(); 69 try 70 { 71 stream = (OutputStream )m_context.get( streamName ); 72 } 73 catch( Exception e) 74 { 75 throw new ConfigurationException( "Error resolving stream '" + 76 streamName + "' at " + 77 streamConfig.getLocation(), e ); 78 } 79 } 80 81 final Configuration formatterConf = configuration.getChild( "format" ); 82 final Formatter formatter = getFormatter( formatterConf ); 83 84 return new StreamTarget( stream, formatter ); 85 } 86 87 88 public void contextualize( final Context context ) 89 throws ContextException 90 { 91 final DefaultContext newContext = new DefaultContext( context ); 93 94 newContext.put( "System.out", System.out ); 95 newContext.put( "System.err", System.err ); 96 97 super.contextualize( newContext ); 98 } 99 100 protected Formatter getFormatter( final Configuration conf ) 101 { 102 Formatter formatter = null; 103 104 if ( null != conf ) 105 { 106 final FormatterFactory formatterFactory = new FormatterFactory(); 107 formatter = formatterFactory.createFormatter( conf ); 108 } 109 110 return formatter; 111 } 112 } 113 114 | Popular Tags |