1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 14 * implied. 15 * 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 package org.apache.avalon.excalibur.logger.logkit; 20 21 import org.apache.avalon.framework.logger.Logger; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.excalibur.logger.LoggerManager; 24 import org.apache.avalon.framework.logger.LogKitLogger; 25 import org.apache.log.Hierarchy; 26 27 /** 28 * This class sits on top of an existing LogKit Hierarchy 29 * and returns logger wrapping LogKit loggers. 30 * 31 * Attach PrefixDecorator and/or CachingDecorator if desired. 32 * 33 * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a> 34 * @version CVS $Revision: 1.5 $ $Date: 2004/03/10 13:54:51 $ 35 * @since 4.0 36 */ 37 public class LogKitAdapter extends AbstractLogEnabled implements LoggerManager 38 { 39 /** 40 * The hierarchy that really produces loggers. 41 */ 42 protected final Hierarchy m_hierarchy; 43 44 /** 45 * Initialized <code>LogKitAdapter</code> to operate 46 * of a certain LogKit <code>Hierarchy</code>. 47 */ 48 public LogKitAdapter( final Hierarchy hierarchy ) 49 { 50 if ( hierarchy == null ) throw new NullPointerException( "hierarchy" ); 51 m_hierarchy = hierarchy; 52 } 53 54 /** 55 * Return the Logger for the specified category. 56 * <p> 57 * 58 * In LogKit getRootLogger() and getLoggerFor("") 59 * unless the logger for category "" has been explicitly 60 * configured return identically configured but different 61 * loggers. 62 * 63 * <p> 64 * Our LogKitConfHelper configures getRootLogger(), not getLoggerFor(""). 65 * We think this is a reasonable behavior and expect that LogKit 66 * Hierarchies configured by other means then LogKitConfHelper are 67 * configured in the same way. 68 * 69 * <p> 70 * This justifies our decision to return getRootLogger() when given 71 * "" category name. 72 */ 73 public Logger getLoggerForCategory( final String categoryName ) 74 { 75 if ( categoryName == null || categoryName.length() == 0 ) 76 { 77 return getDefaultLogger(); 78 } 79 else 80 { 81 return new LogKitLogger( m_hierarchy.getLoggerFor( categoryName ) ); 82 } 83 } 84 85 /** 86 * Return the default Logger. This is basically the same 87 * as getting the Logger for the "" category. 88 */ 89 public Logger getDefaultLogger() 90 { 91 return new LogKitLogger( m_hierarchy.getRootLogger() ); 92 } 93 } 94