KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > DefaultFormatterFactory


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.logkit;
19
20 import org.apache.avalon.framework.configuration.Configuration;
21
22 import org.apache.avalon.util.i18n.ResourceManager;
23 import org.apache.avalon.util.i18n.Resources;
24
25 import org.apache.log.format.ExtendedPatternFormatter;
26 import org.apache.log.format.Formatter;
27 import org.apache.log.format.PatternFormatter;
28 import org.apache.log.format.RawFormatter;
29 import org.apache.log.format.SyslogFormatter;
30 import org.apache.log.format.XMLFormatter;
31
32 /**
33  * Factory for Formatter-s.
34  */

35 public class DefaultFormatterFactory implements FormatterFactory
36 {
37     //--------------------------------------------------------------
38
// static
39
//--------------------------------------------------------------
40

41     private static final Resources REZ =
42       ResourceManager.getPackageResources( DefaultFormatterFactory.class );
43     
44     //--------------------------------------------------------------
45
// implementation
46
//--------------------------------------------------------------
47

48    /**
49     * Creation of a new formatter using a supplied configuration.
50     * @param config the formatter configuration
51     * @return the formatter instance
52     * @exception IllegalArgumentException if the formatter type is unknown
53     */

54     public Formatter createFormatter( final Configuration config )
55     {
56         if( null == config ) return new StandardFormatter( DEFAULT_FORMAT );
57         final String JavaDoc type = config.getAttribute( "type", "pattern" );
58         final String JavaDoc format = config.getValue( DEFAULT_FORMAT );
59         return createFormatter( type, format );
60     }
61
62    /**
63     * Creation of a new formatter.
64     * @param type the formatter type identifier
65     * @param format the format specification
66     * @return the formatter instance
67     * @exception IllegalArgumentException if the formatter type is unknown
68     */

69     public Formatter createFormatter( String JavaDoc type, String JavaDoc format )
70     {
71         if( "avalon".equals( type ) )
72         {
73             return new StandardFormatter( format, true );
74         }
75         else if( "console".equals( type ) )
76         {
77             return new StandardFormatter( format, false );
78         }
79         else if( "extended".equals( type ) )
80         {
81             //
82
// Normally ExtendPatternFormatter would look for callers
83
// of Logger.class. But when Excalibur Logger provides a
84
// facade, the user class/method is actually one-level deeper.
85
// We therefore create the pattern-formatter with an
86
// additional depth-offset of 1.
87
//
88

89             return new ExtendedPatternFormatter( format, 1 );
90         }
91         else if( "raw".equals( type ) )
92         {
93             return new RawFormatter();
94         }
95         else if( "xml".equals( type ) )
96         {
97             return new XMLFormatter();
98         }
99         else if( "syslog".equals( type ) )
100         {
101             return new SyslogFormatter();
102         }
103         else if( "pattern".equals( type ) )
104         {
105             return new PatternFormatter( format );
106         }
107         else
108         {
109             final String JavaDoc error =
110               REZ.getString( "formatter.error.unknown-type", type );
111             throw new IllegalArgumentException JavaDoc( error );
112         }
113     }
114 }
115
Popular Tags