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.Loggable; 22 import org.apache.log.Hierarchy; 23 import org.apache.log.LogTarget; 24 import org.apache.log.Logger; 25 import org.apache.log.Priority; 26 27 38 public class DefaultLogKitManager 39 extends AbstractLogEnabled 40 implements LogKitManager, Loggable, Contextualizable, Configurable 41 { 42 43 final private Map m_loggers = new HashMap (); 44 45 46 private Context m_context; 47 48 49 private Hierarchy m_hierarchy; 50 51 52 private String m_prefix; 53 54 57 public DefaultLogKitManager() 58 { 59 this( new Hierarchy() ); 60 } 61 62 65 public DefaultLogKitManager( final Hierarchy hierarchy ) 66 { 67 this( null, hierarchy ); 68 } 69 70 74 public DefaultLogKitManager( final String prefix ) 75 { 76 this( prefix, new Hierarchy() ); 77 } 78 79 83 public DefaultLogKitManager( final String prefix, final Hierarchy hierarchy ) 84 { 85 m_prefix = prefix; 86 m_hierarchy = hierarchy; 87 } 88 89 public void setLogger( final Logger logger ) 90 { 91 enableLogging( new LogKitLogger( logger ) ); 92 } 93 94 103 public final Logger getLogger( final String categoryName ) 104 { 105 final Logger logger = (Logger) m_loggers.get( categoryName ); 106 107 if( null != logger ) 108 { 109 if( getLogger().isDebugEnabled() ) 110 { 111 getLogger().debug( "Logger for category " + categoryName + " returned" ); 112 } 113 return logger; 114 } 115 116 if( getLogger().isDebugEnabled() ) 117 { 118 getLogger().debug( "Logger for category " + categoryName 119 + " not defined in configuration. New Logger created and returned" ); 120 } 121 122 return m_hierarchy.getLoggerFor( categoryName ); 123 } 124 125 130 public Hierarchy getHierarchy() 131 { 132 return m_hierarchy; 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( getLogger() ); 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( getLogger() ); 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 + Logger.CATEGORY_SEPARATOR + category; 279 } 280 281 final Logger logger = m_hierarchy.getLoggerFor( full_category ); 282 m_loggers.put( full_category, logger ); 283 if( getLogger().isDebugEnabled() ) 284 { 285 getLogger().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 |