KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > Log4JConfLoggerManager


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;
20
21 import org.apache.avalon.excalibur.logger.log4j.Log4JConfigurator;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.configuration.ConfigurationUtil;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.log4j.LogManager;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 /**
36  * A LoggerManager for Log4j that will configure the Log4j subsystem
37  * using specified configuration.
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  * @version $Revision: 1.8 $ $Date: 2004/05/04 11:14:28 $
41  */

42 public class Log4JConfLoggerManager
43     extends Log4JLoggerManager
44     implements Configurable, Contextualizable
45 {
46     private Context m_context;
47     
48
49     /* (non-Javadoc)
50      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
51      */

52     public void contextualize(Context context) throws ContextException
53     {
54         m_context = context;
55     }
56     
57     /**
58      * Work around a weird compilation problem. Can not call
59      * the constructor from fortress/ContextManager, get a
60      * file org\apache\log4j\spi\LoggerRepository.class not found
61      * new Log4JConfLoggerManager( lmDefaultLoggerName, lmLoggerName );
62      */

63     public static Log4JConfLoggerManager newInstance( final String JavaDoc prefix,
64             final String JavaDoc switchToCategory )
65     {
66         return new Log4JConfLoggerManager( prefix, switchToCategory );
67     }
68
69     public Log4JConfLoggerManager( final String JavaDoc prefix, final String JavaDoc switchToCategory )
70     {
71         super( prefix, switchToCategory );
72     }
73
74     public Log4JConfLoggerManager()
75     {
76     }
77
78     public void configure( final Configuration configuration )
79         throws ConfigurationException
80     {
81         final Element JavaDoc element = ConfigurationUtil.toElement( configuration );
82         final Document JavaDoc document = element.getOwnerDocument();
83         final Element JavaDoc newElement = document.createElement( "log4j:configuration" );
84         final NodeList JavaDoc childNodes = element.getChildNodes();
85         final int length = childNodes.getLength();
86         for( int i = 0; i < length; i++ )
87         {
88             final Node JavaDoc node = childNodes.item( i );
89             final Node JavaDoc newNode = node.cloneNode( true );
90             newElement.appendChild( newNode );
91         }
92
93         document.appendChild( newElement );
94         
95         Log4JConfigurator configurator = new Log4JConfigurator(m_context);
96         configurator.doConfigure( newElement, LogManager.getLoggerRepository());
97     }
98 }
99
Popular Tags