1 8 package org.apache.avalon.excalibur.logger; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 import org.apache.avalon.framework.configuration.Configurable; 13 import org.apache.avalon.framework.configuration.Configuration; 14 import org.apache.avalon.framework.configuration.ConfigurationException; 15 import org.apache.avalon.framework.context.Context; 16 import org.apache.avalon.framework.context.ContextException; 17 import org.apache.avalon.framework.context.Contextualizable; 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.logger.LogEnabled; 20 import org.apache.avalon.framework.logger.LogKitLogger; 21 import org.apache.avalon.framework.logger.Logger; 22 import org.apache.log.Hierarchy; 23 import org.apache.log.LogTarget; 24 import org.apache.log.Priority; 25 26 35 public class LogKitLoggerManager 36 implements LoggerManager, Contextualizable, Configurable 37 { 38 39 final private Map m_loggers = new HashMap (); 40 41 42 private Context m_context; 43 44 45 private Hierarchy m_hierarchy; 46 47 48 private String m_prefix; 49 50 51 final private Logger m_defaultLogger; 52 53 56 public LogKitLoggerManager() 57 { 58 this( new Hierarchy() ); 59 } 60 61 64 public LogKitLoggerManager( final Hierarchy hierarchy ) 65 { 66 this( null, hierarchy ); 67 } 68 69 73 public LogKitLoggerManager( final String prefix ) 74 { 75 this( prefix, new Hierarchy() ); 76 } 77 78 82 public LogKitLoggerManager( final String prefix, final Hierarchy hierarchy ) 83 { 84 this( prefix, hierarchy, 85 new LogKitLogger( hierarchy.getLoggerFor("") ) ); 86 } 87 88 92 public LogKitLoggerManager( final String prefix, final Hierarchy hierarchy, final Logger defaultLogger ) 93 { 94 m_prefix = prefix; 95 m_hierarchy = hierarchy; 96 m_defaultLogger = defaultLogger; 97 } 98 99 108 public final Logger getLoggerForCategory( final String categoryName ) 109 { 110 final Logger logger = (Logger) m_loggers.get( categoryName ); 111 112 if( null != logger ) 113 { 114 if( m_defaultLogger.isDebugEnabled() ) 115 { 116 m_defaultLogger.debug( "Logger for category " + categoryName + " returned" ); 117 } 118 return logger; 119 } 120 121 if( m_defaultLogger.isDebugEnabled() ) 122 { 123 m_defaultLogger.debug( "Logger for category " + categoryName 124 + " not defined in configuration. New Logger created and returned" ); 125 } 126 127 return new LogKitLogger(m_hierarchy.getLoggerFor( categoryName )); 128 } 129 130 public final Logger getDefaultLogger() 131 { 132 return m_defaultLogger; 133 } 134 135 141 public final void contextualize( final Context context ) 142 throws ContextException 143 { 144 m_context = context; 145 } 146 147 153 public final void configure( final Configuration configuration ) 154 throws ConfigurationException 155 { 156 final Configuration factories = configuration.getChild( "factories" ); 157 final LogTargetFactoryManager targetFactoryManager = setupTargetFactoryManager( factories ); 158 159 final Configuration targets = configuration.getChild( "targets" ); 160 final LogTargetManager targetManager = setupTargetManager( targets, targetFactoryManager ); 161 162 final Configuration categories = configuration.getChild( "categories" ); 163 final Configuration [] category = categories.getChildren( "category" ); 164 setupLoggers( targetManager, m_prefix, category ); 165 } 166 167 173 private final LogTargetFactoryManager setupTargetFactoryManager( final Configuration configuration ) 174 throws ConfigurationException 175 { 176 final DefaultLogTargetFactoryManager targetFactoryManager = new DefaultLogTargetFactoryManager(); 177 if( targetFactoryManager instanceof LogEnabled ) 178 { 179 targetFactoryManager.enableLogging( m_defaultLogger ); 180 } 181 182 if( targetFactoryManager instanceof Contextualizable ) 183 { 184 try 185 { 186 targetFactoryManager.contextualize( m_context ); 187 } 188 catch( final ContextException ce ) 189 { 190 throw new ConfigurationException( "cannot contextualize default factory manager", ce ); 191 } 192 } 193 194 targetFactoryManager.configure( configuration ); 195 196 return targetFactoryManager; 197 } 198 199 205 private final LogTargetManager setupTargetManager( final Configuration configuration, 206 final LogTargetFactoryManager targetFactoryManager ) 207 throws ConfigurationException 208 { 209 final DefaultLogTargetManager targetManager = new DefaultLogTargetManager(); 210 211 if (targetManager instanceof LogEnabled) 212 { 213 targetManager.enableLogging( m_defaultLogger ); 214 } 215 216 if( targetManager instanceof Contextualizable ) 217 { 218 try 219 { 220 targetManager.contextualize( m_context ); 221 } 222 catch( final ContextException ce ) 223 { 224 throw new ConfigurationException( "cannot contextualize factory manager", ce ); 225 } 226 } 227 228 if( targetManager instanceof LogTargetFactoryManageable ) 229 { 230 targetManager.setLogTargetFactoryManager( targetFactoryManager ); 231 } 232 233 if( targetManager instanceof Configurable ) 234 { 235 targetManager.configure( configuration ); 236 } 237 238 return targetManager; 239 } 240 241 247 private final void setupLoggers( final LogTargetManager targetManager, 248 final String parentCategory, 249 final Configuration [] categories ) 250 throws ConfigurationException 251 { 252 for( int i = 0; i < categories.length; i++ ) 253 { 254 final String category = categories[i].getAttribute( "name" ); 255 final String loglevel = categories[i].getAttribute( "log-level" ).toUpperCase(); 256 257 final Configuration [] targets = categories[i].getChildren( "log-target" ); 258 final LogTarget [] log_targets = new LogTarget[ targets.length ]; 259 for( int j = 0; j < targets.length; j++ ) 260 { 261 final String id = targets[j].getAttribute( "id-ref" ); 262 log_targets[j] = targetManager.getLogTarget( id ); 263 } 264 265 if( "".equals( category ) && log_targets.length > 0 ) 266 { 267 m_hierarchy.setDefaultPriority( Priority.getPriorityForName( loglevel ) ); 268 m_hierarchy.setDefaultLogTargets( log_targets ); 269 } 270 271 final String full_category; 272 if( null == parentCategory ) 273 { 274 full_category = category; 275 } 276 else 277 { 278 full_category = parentCategory + org.apache.log.Logger.CATEGORY_SEPARATOR + category; 279 } 280 281 final org.apache.log.Logger logger = m_hierarchy.getLoggerFor( full_category ); 282 m_loggers.put( full_category, new LogKitLogger( logger ) ); 283 if( m_defaultLogger.isDebugEnabled() ) 284 { 285 m_defaultLogger.debug( "added logger for category " + full_category ); 286 } 287 logger.setPriority( Priority.getPriorityForName( loglevel ) ); 288 logger.setLogTargets( log_targets ); 289 290 final Configuration [] sub_categories = categories[i].getChildren( "category" ); 291 if( null != sub_categories ) 292 { 293 setupLoggers( targetManager, full_category, sub_categories ); 294 } 295 } 296 } 297 } 298 | Popular Tags |