1 8 package org.apache.avalon.phoenix.components.logger; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.util.HashMap ; 13 import org.apache.avalon.excalibur.i18n.ResourceManager; 14 import org.apache.avalon.excalibur.i18n.Resources; 15 import org.apache.avalon.excalibur.logger.LoggerManager; 16 import org.apache.avalon.framework.configuration.Configurable; 17 import org.apache.avalon.framework.configuration.Configuration; 18 import org.apache.avalon.framework.configuration.ConfigurationException; 19 import org.apache.avalon.framework.context.Context; 20 import org.apache.avalon.framework.context.ContextException; 21 import org.apache.avalon.framework.context.Contextualizable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.logger.AvalonFormatter; 24 import org.apache.avalon.framework.logger.LogKitLogger; 25 import org.apache.log.Hierarchy; 26 import org.apache.log.LogTarget; 27 import org.apache.log.Logger; 28 import org.apache.log.Priority; 29 import org.apache.log.output.io.FileTarget; 30 31 38 public class SimpleLogKitManager 39 extends AbstractLogEnabled 40 implements LoggerManager, Contextualizable, Configurable 41 { 42 private static final Resources REZ = 43 ResourceManager.getPackageResources( SimpleLogKitManager.class ); 44 45 private static final String DEFAULT_FORMAT = 46 "%7.7{priority} %23.23{time:yyyy-MM-dd' 'HH:mm:ss.SSS} [%8.8{category}] (%{context}): " 47 + "%{message}\n%{throwable}"; 48 49 private File m_baseDirectory; 51 52 54 private final Hierarchy m_hierarchy = new Hierarchy(); 55 56 private final Logger m_logkitLogger = m_hierarchy.getLoggerFor( "" ); 57 58 private org.apache.avalon.framework.logger.Logger m_logger = 59 new LogKitLogger( m_logkitLogger ); 60 61 public void contextualize( final Context context ) 62 throws ContextException 63 { 64 m_baseDirectory = (File )context.get( "app.home" ); 65 } 66 67 public void configure( final Configuration configuration ) 68 throws ConfigurationException 69 { 70 final Configuration[] targets = configuration.getChildren( "log-target" ); 71 final HashMap targetSet = configureTargets( targets ); 72 final Configuration[] categories = configuration.getChildren( "category" ); 73 configureCategories( categories, targetSet ); 74 } 75 76 public org.apache.avalon.framework.logger.Logger 77 getLoggerForCategory( final String categoryName ) 78 { 79 return m_logger.getChildLogger( categoryName ); 80 } 81 82 public org.apache.avalon.framework.logger.Logger getDefaultLogger() 83 { 84 return m_logger; 85 } 86 87 94 private HashMap configureTargets( final Configuration[] targets ) 95 throws ConfigurationException 96 { 97 final HashMap targetSet = new HashMap (); 98 99 for( int i = 0; i < targets.length; i++ ) 100 { 101 final Configuration target = targets[ i ]; 102 final String name = target.getAttribute( "name" ); 103 String location = target.getAttribute( "location" ).trim(); 104 final String format = target.getAttribute( "format", DEFAULT_FORMAT ); 105 106 if( '/' == location.charAt( 0 ) ) 107 { 108 location = location.substring( 1 ); 109 } 110 111 final AvalonFormatter formatter = new AvalonFormatter( format ); 112 113 final File file = new File ( m_baseDirectory, location ); 115 116 FileTarget logTarget = null; 118 try 119 { 120 logTarget = new FileTarget( file.getAbsoluteFile(), false, formatter ); 121 } 122 catch( final IOException ioe ) 123 { 124 final String message = 125 REZ.getString( "target.nocreate", name, file, ioe.getMessage() ); 126 throw new ConfigurationException( message, ioe ); 127 } 128 129 targetSet.put( name, logTarget ); 130 } 131 132 return targetSet; 133 } 134 135 142 private void configureCategories( final Configuration[] categories, final HashMap targets ) 143 throws ConfigurationException 144 { 145 for( int i = 0; i < categories.length; i++ ) 146 { 147 final Configuration category = categories[ i ]; 148 final String name = category.getAttribute( "name", "" ); 149 final String target = category.getAttribute( "target" ); 150 final String priorityName = category.getAttribute( "priority" ); 151 152 final Logger logger = 153 m_logkitLogger.getChildLogger( name ); 154 155 final LogTarget logTarget = (LogTarget)targets.get( target ); 156 if( null == target ) 157 { 158 final String message = REZ.getString( "unknown-target", target, name ); 159 throw new ConfigurationException( message ); 160 } 161 162 final Priority priority = Priority.getPriorityForName( priorityName ); 163 if( !priority.getName().equals( priorityName ) ) 164 { 165 final String message = REZ.getString( "unknown-priority", priorityName, name ); 166 throw new ConfigurationException( message ); 167 } 168 169 if( getLogger().isDebugEnabled() ) 170 { 171 final String message = 172 REZ.getString( "category-create", name, target, priorityName ); 173 getLogger().debug( message ); 174 } 175 176 if( name.equals( "" ) ) 177 { 178 m_hierarchy.setDefaultPriority( priority ); 179 m_hierarchy.setDefaultLogTarget( logTarget ); 180 } 181 else 182 { 183 logger.setPriority( priority ); 184 logger.setLogTargets( new LogTarget[]{logTarget} ); 185 } 186 } 187 } 188 } 189 | Popular Tags |