1 19 package org.apache.avalon.excalibur.logger.logkit; 20 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.avalon.excalibur.logger.DefaultLogTargetFactoryManager; 26 import org.apache.avalon.excalibur.logger.DefaultLogTargetManager; 27 import org.apache.avalon.excalibur.logger.LogTargetFactoryManager; 28 import org.apache.avalon.excalibur.logger.LogTargetManager; 29 import org.apache.avalon.excalibur.logger.util.LoggerUtil; 30 import org.apache.avalon.framework.activity.Disposable; 31 import org.apache.avalon.framework.configuration.Configurable; 32 import org.apache.avalon.framework.configuration.Configuration; 33 import org.apache.avalon.framework.configuration.ConfigurationException; 34 import org.apache.avalon.framework.container.ContainerUtil; 35 import org.apache.avalon.framework.context.Context; 36 import org.apache.avalon.framework.context.ContextException; 37 import org.apache.avalon.framework.context.Contextualizable; 38 import org.apache.avalon.framework.logger.AbstractLogEnabled; 39 import org.apache.log.Hierarchy; 40 import org.apache.log.LogTarget; 41 import org.apache.log.Priority; 42 import org.apache.log.util.Closeable; 43 44 55 public class LogKitConfHelper extends AbstractLogEnabled implements 56 Contextualizable, 57 Configurable, 58 Disposable 59 { 60 61 private final Hierarchy m_hierarchy; 62 63 64 public LogKitConfHelper( final Hierarchy hierarchy ) 65 { 66 if ( hierarchy == null ) throw new NullPointerException ( "hierarchy" ); 67 m_hierarchy = hierarchy; 68 } 69 70 71 final private Set m_targets = new HashSet (); 72 73 74 private Context m_context; 75 76 82 public final void contextualize( final Context context ) 83 throws ContextException 84 { 85 m_context = context; 86 } 87 88 94 public final void configure( final Configuration configuration ) 95 throws ConfigurationException 96 { 97 final Configuration factories = configuration.getChild( "factories" ); 98 final LogTargetFactoryManager targetFactoryManager = setupTargetFactoryManager( factories ); 99 100 final Configuration targets = configuration.getChild( "targets" ); 101 final LogTargetManager targetManager = setupTargetManager( targets, targetFactoryManager ); 102 103 final Configuration categories = configuration.getChild( "categories" ); 104 setupLoggers( targetManager, 105 null, 106 categories, 107 true, 108 categories.getAttributeAsBoolean( "additive", false ) ); 109 } 110 111 117 private final LogTargetFactoryManager setupTargetFactoryManager( 118 final Configuration configuration ) 119 throws ConfigurationException 120 { 121 final DefaultLogTargetFactoryManager targetFactoryManager = new DefaultLogTargetFactoryManager(); 122 123 ContainerUtil.enableLogging( targetFactoryManager, getLogger() ); 124 125 try 126 { 127 ContainerUtil.contextualize( targetFactoryManager, m_context ); 128 } 129 catch( final ContextException ce ) 130 { 131 throw new ConfigurationException( "cannot contextualize default factory manager", ce ); 132 } 133 134 ContainerUtil.configure( targetFactoryManager, configuration ); 135 136 return targetFactoryManager; 137 } 138 139 145 private final LogTargetManager setupTargetManager( final Configuration configuration, 146 final LogTargetFactoryManager targetFactoryManager ) 147 throws ConfigurationException 148 { 149 final DefaultLogTargetManager targetManager = new DefaultLogTargetManager(); 150 151 ContainerUtil.enableLogging( targetManager, getLogger() ); 152 153 targetManager.setLogTargetFactoryManager( targetFactoryManager ); 154 155 ContainerUtil.configure( targetManager, configuration ); 156 157 return targetManager; 158 } 159 160 167 private final void setupLoggers( final LogTargetManager targetManager, 168 final String parentCategory, 169 final Configuration parentElement, 170 boolean root, 171 final boolean defaultAdditive ) 172 throws ConfigurationException 173 { 174 boolean rootLoggerConfigured = false; 175 176 final Configuration[] categories = parentElement.getChildren( "category" ); 177 178 if( null != categories ) 179 { 180 for( int i = 0; i < categories.length; i++ ) 181 { 182 final Configuration category = categories[ i ]; 183 final String name = category.getAttribute( "name" ); 184 final String loglevel = category.getAttribute( "log-level" ).toUpperCase(); 185 final boolean additive = category. 186 getAttributeAsBoolean( "additive", defaultAdditive ); 187 188 final Configuration[] targets = category.getChildren( "log-target" ); 189 final LogTarget[] logTargets = new LogTarget[ targets.length ]; 190 for( int j = 0; j < targets.length; j++ ) 191 { 192 final String id = targets[ j ].getAttribute( "id-ref" ); 193 logTargets[ j ] = targetManager.getLogTarget( id ); 194 if( !m_targets.contains( logTargets[ j ] ) ) 195 { 196 m_targets.add( logTargets[ j ] ); 197 } 198 } 199 200 final String fullCategory; 201 final org.apache.log.Logger logger; 202 203 if ( "".equals( name ) ) 204 { 205 if ( !root ) 206 { 207 final String message = "'category' element with empty name not " + 208 "at the root level: " + category.getLocation(); 209 throw new ConfigurationException( message ); 210 } 211 212 if ( logTargets.length == 0 ) 213 { 214 final String message = "At least one log-target should be " + 215 "specified for the root category " + category.getLocation(); 216 throw new ConfigurationException( message ); 217 } 218 219 fullCategory = null; 220 logger = m_hierarchy.getRootLogger(); 221 rootLoggerConfigured = true; 222 } 223 else 224 { 225 fullCategory = LoggerUtil.getFullCategoryName( parentCategory, name ); 226 logger = m_hierarchy.getLoggerFor( fullCategory ); 227 } 228 229 if( getLogger().isDebugEnabled() ) 230 { 231 236 final String message = "LogKitConfHelper: adding logger for category '" + 237 ( fullCategory != null ? fullCategory : "" ) + "'"; 238 getLogger().debug( message ); 239 } 240 241 logger.setPriority( Priority.getPriorityForName( loglevel ) ); 242 logger.setLogTargets( logTargets ); 243 logger.setAdditivity( additive ); 244 245 setupLoggers( targetManager, fullCategory, category, false, defaultAdditive ); 246 } 247 } 248 249 if ( root && !rootLoggerConfigured ) 250 { 251 final String message = 252 "No configuration for root category (<category name=''/>) found in "+ 253 parentElement.getLocation(); 254 throw new ConfigurationException( message ); 255 } 256 } 257 258 261 public void dispose() 262 { 263 final Iterator iterator = m_targets.iterator(); 264 while( iterator.hasNext() ) 265 { 266 final LogTarget target = (LogTarget)iterator.next(); 267 if( target instanceof Closeable ) 268 { 269 ( (Closeable)target ).close(); 270 } 271 } 272 } 273 } 274 | Popular Tags |