KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > decorator > PrefixDecorator


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.decorator;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.avalon.excalibur.logger.LoggerManager;
23 import org.apache.avalon.excalibur.logger.util.LoggerUtil;
24
25 /**
26  * This class implements LoggerManager interface by
27  * prepending a prefix to all requests and letting the
28  * wrapped LoggerManager actually create the loggers.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version CVS $Revision: 1.3 $ $Date: 2004/03/10 13:54:50 $
32  * @since 4.0
33  */

34 public class PrefixDecorator extends LoggerManagerDecorator implements LoggerManager
35 {
36     private final String JavaDoc m_prefix;
37
38     /**
39      * Creates a PrefixDecorator instance.
40      * @param prefix the prefix to prepend;
41      * can be neither null nor empty.
42      * This is done to avoid ambiguity
43      * in the getDefaultLogger() method - what would we call
44      * in such case getDefaultLogger() or getLoggerForCategory("") ?
45      *
46      */

47     public PrefixDecorator( final LoggerManager loggerManager, final String JavaDoc prefix )
48     {
49         super( loggerManager );
50         if ( prefix == null ) throw new NullPointerException JavaDoc( "prefix" );
51         if ( "".equals( prefix ) ) throw new IllegalArgumentException JavaDoc( "prefix can't be empty" );
52         m_prefix = prefix;
53     }
54
55     /**
56      * Return the Logger for the specified category.
57      */

58     public Logger getLoggerForCategory( final String JavaDoc categoryName )
59     {
60         final String JavaDoc fullCategoryName = LoggerUtil.getFullCategoryName( m_prefix, categoryName );
61         return m_loggerManager.getLoggerForCategory( fullCategoryName );
62     }
63
64     /**
65      * Return the default Logger. This is basically the same
66      * as getting the Logger for the "" category.
67      */

68     public Logger getDefaultLogger()
69     {
70         final String JavaDoc fullCategoryName = LoggerUtil.getFullCategoryName( m_prefix, null );
71         return m_loggerManager.getLoggerForCategory( fullCategoryName );
72     }
73 }
74
Popular Tags