KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > impl > LoggerParameter


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.logging.impl;
19
20 import org.apache.avalon.framework.logger.Logger;
21
22 import org.apache.avalon.util.criteria.Parameter;
23 import org.apache.avalon.util.criteria.CriteriaException;
24
25 import org.apache.avalon.util.i18n.ResourceManager;
26 import org.apache.avalon.util.i18n.Resources;
27
28 /**
29  * A parameter descriptor that supports transformation of a
30  * a string to a Logger instance.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version $Revision: 1.3 $
34  */

35 public class LoggerParameter extends Parameter
36 {
37     //--------------------------------------------------------------
38
// static
39
//--------------------------------------------------------------
40

41     private static final Resources REZ =
42       ResourceManager.getPackageResources( LoggerParameter.class );
43
44     private static final int PRIORITY = ConsoleLogger.LEVEL_WARN;
45
46     //--------------------------------------------------------------
47
// constructors
48
//--------------------------------------------------------------
49

50    /**
51     * Creation of a new logger parameter. The parameter support
52     * convertion of strings in the form "debug", "info", "warn",
53     * "error", "fatal" and "none" to an equivalent logger.
54     *
55     * @param key the parameter key
56     * @param logger the default logger
57     */

58     public LoggerParameter( final String JavaDoc key, final Logger logger )
59     {
60         super( key, Logger.class, logger );
61     }
62
63    /**
64     * Resolve a supplied string to a configuration
65     * @param value the value to resolve
66     * @exception CriteriaException if an error occurs
67     */

68     public Object JavaDoc resolve( Object JavaDoc value )
69       throws CriteriaException
70     {
71         if( value == null )
72         {
73             return new ConsoleLogger( PRIORITY );
74         }
75         if( value instanceof Logger )
76         {
77             return value;
78         }
79         if( value instanceof String JavaDoc )
80         {
81             String JavaDoc priority = ((String JavaDoc)value).toLowerCase();
82             if( priority.equals( "debug" ) )
83             {
84                 return new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG );
85             }
86             else if( priority.equals( "info" ) )
87             {
88                 return new ConsoleLogger( ConsoleLogger.LEVEL_INFO );
89             }
90             else if( priority.equals( "warn" ) )
91             {
92                 return new ConsoleLogger( ConsoleLogger.LEVEL_WARN );
93             }
94             else if( priority.equals( "error" ) )
95             {
96                 return new ConsoleLogger( ConsoleLogger.LEVEL_ERROR );
97             }
98             else if( priority.equals( "fatal" ) )
99             {
100                 return new ConsoleLogger( ConsoleLogger.LEVEL_FATAL );
101             }
102             else if( priority.equals( "none" ) )
103             {
104                 return new ConsoleLogger( ConsoleLogger.LEVEL_DISABLED );
105             }
106         }
107         final String JavaDoc error =
108           REZ.getString(
109             "parameter.unknown",
110             value.getClass().getName(), Logger.class.getName() );
111         throw new CriteriaException( error );
112     }
113 }
114
Popular Tags