KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > log4j > Log4JConfAdapter


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.log4j;
20
21 import org.apache.avalon.framework.configuration.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.configuration.ConfigurationUtil;
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.ContextException;
27 import org.apache.avalon.framework.context.Contextualizable;
28 import org.apache.log4j.Hierarchy;
29 import org.apache.log4j.Level;
30 import org.apache.log4j.spi.RootCategory;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36 /**
37  * A LoggerManager for Log4j that will configure the Log4j subsystem
38  * using specified configuration.
39  * <p>
40  * Note that in case of logging errors Log4J will (via the
41  * org.apache.log4j.helpers.LogLog class) write to System.err.
42  * This can be switched off but we can not substitute our
43  * own handler to log erros the way we prefer to do this. :-(
44  *
45  * <p>
46  * So, unlike the LogKit case we have no Log4JLogger helper to
47  * implement and hence a different architecture: this class
48  * is not a helper but a regular subclass of Log4JAdapter.
49  *
50  * <p>
51  * Attach PrefixDecorator and/or CachingDecorator if desired.
52  *
53  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
54  * @version CVS $Revision: 1.4 $ $Date: 2004/03/10 13:54:51 $
55  * @since 4.0
56  */

57 public class Log4JConfAdapter extends Log4JAdapter
58 implements Configurable, Contextualizable
59 {
60     private Context m_context;
61     
62     
63     /* (non-Javadoc)
64      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
65      */

66     public void contextualize(Context context) throws ContextException
67     {
68         m_context = context;
69     }
70     
71     /**
72      * This constructor creates a completely independent
73      * Log4J hierarchy. If you want to log to an existing
74      * Log4J hierarchy please use Log4JAdapter. This class
75      * always creates a new private hierarchy and configures
76      * it all by itself.
77      */

78     public Log4JConfAdapter()
79     {
80         /**
81          * Copied from org.apache.log4j.LogManager.
82          */

83         super( new Hierarchy( new RootCategory( Level.ALL ) ) );
84     }
85
86     /**
87      * Feed our configuration to Log4J.
88      */

89     public void configure( final Configuration configuration )
90         throws ConfigurationException
91     {
92         final Element JavaDoc element = ConfigurationUtil.toElement( configuration );
93         final Document JavaDoc document = element.getOwnerDocument();
94         final Element JavaDoc newElement = document.createElement( "log4j:configuration" );
95         final NodeList JavaDoc childNodes = element.getChildNodes();
96         final int length = childNodes.getLength();
97         for( int i = 0; i < length; i++ )
98         {
99             final Node JavaDoc node = childNodes.item( i );
100             final Node JavaDoc newNode = node.cloneNode( true );
101             newElement.appendChild( newNode );
102         }
103
104         document.appendChild( newElement );
105
106         /**
107          * Copied from org.apache.log4j.xml.DomConfigurator configure().
108          * We want our own hierarchy to be configured, so we shall
109          * be a bit more elaborate then just calling configure().
110          */

111         final Log4JConfigurator domConfigurator = new Log4JConfigurator( m_context );
112         domConfigurator.doConfigure( newElement, m_hierarchy );
113     }
114 }
115
Popular Tags