KickJava   Java API By Example, From Geeks To Geeks.

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


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.framework.container.ContainerUtil;
23 import org.apache.avalon.excalibur.logger.LoggerManager;
24 import org.apache.avalon.excalibur.logger.util.LoggerSwitch;
25
26 /**
27  *
28  * This class intercepts the class passed to us via
29  * <code>enableLogging()</code> and substibutes it
30  * by <code>LoggerSwitch.get()</code> logger.
31  * <p>
32  * Later on at the <code>start()</code> stage
33  * we assume that our wrapped LoggerManager has already
34  * completely initialized itself and extract
35  * a <code>Logger</code> from it.
36  * <p>
37  * <code>LoggerSwitch</code> allowes us to supply this
38  * logger to it via <code>LoggerSwitch.setPreferred()</code>.
39  * This has the effect of all the log messages directed
40  * to <code>LoggerSwitch.get()</code> obtained logger
41  * being directed to the new <code>Logger</code> unless
42  * a recursion error happens.
43  *
44  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
45  * @version CVS $Revision: 1.4 $ $Date: 2004/03/10 13:54:50 $
46  * @since 4.0
47  */

48 public class LogToSelfDecorator extends LoggerManagerDecorator
49 {
50     /* The category to switch our logging to. */
51     private final String JavaDoc m_switchTo;
52     /* The LoggerSwitch object controlling our substitute Logger. */
53     private LoggerSwitch m_switch;
54     /**
55      * Our substitute logger obtained from m_switch.
56      * Used for our own logging.
57      */

58     private Logger m_logger;
59
60     /**
61      * Creates a LogToSelfDecorator instance.
62      * @param switchTo the name of the category we should extract
63      * a Logger and switch our logging to at the
64      * <code>start()</code> stage; can not be null;
65      * empty value causes logging to be switched to the
66      * "" category.
67      */

68     public LogToSelfDecorator( final LoggerManager loggerManager, final String JavaDoc switchTo )
69     {
70         super( loggerManager );
71         if ( switchTo == null ) throw new NullPointerException JavaDoc( "switchTo" );
72         m_switchTo = switchTo;
73     }
74
75     /**
76      * Substitutes the supplied logger by <code>m_switch.get()</code>.
77      * The substiting logger is used both for our own logging and
78      * passed onto our decorated <code>LoggerManager</code>.
79      * @param logger the logger supplied for us and our wrapped
80      * LoggerManager; we chould survive with a null logger
81      * (LoggerSwitch create a NullLogger in this case), but
82      * for better error detection we shall rather blow up.
83      */

84     public void enableLogging( final Logger logger )
85     {
86         if ( m_switch != null )
87         {
88             throw new IllegalStateException JavaDoc( "enableLogging() already called" );
89         }
90
91         if ( logger == null )
92         {
93             throw new NullPointerException JavaDoc( "logger" );
94         }
95
96         m_switch = new LoggerSwitch( logger );
97         m_logger = m_switch.get();
98         ContainerUtil.enableLogging( m_loggerManager, m_logger );
99     }
100
101     /**
102      * Invokes <code>start()</code> on our wrapped
103      * <code>LoggerManager</code> and swithces the
104      * logger used by us and all objects that we
105      * decorate for a logger extracted from our
106      * wrapped <code>LoggerManager</code>.
107      */

108     public void start() throws Exception JavaDoc
109     {
110         /**
111          * If our LoggerManager is <code>Startable</code>
112          * its <code>start()</code> will be invoked now.
113          */

114         super.start();
115
116         final Logger preferred = m_loggerManager.getLoggerForCategory( m_switchTo );
117         if ( m_logger.isDebugEnabled() )
118         {
119             /**
120              * We have to identify ourselves now via 'LogToSelfDecorator:'
121              * because we are likely to be logging to a shared bootstrap
122              * logger, not to a dedicated category Logger.
123              */

124             final String JavaDoc message = "LogToSelfDecorator: switching logging to '" +
125                     m_switchTo + "'";
126             m_logger.debug( message );
127         }
128         
129         m_switch.setPreferred( preferred );
130
131         if ( m_logger.isDebugEnabled() )
132         {
133             /**
134              * We do not have to identify ourselves now, we're already logging
135              * to a proper category.
136              */

137             final String JavaDoc message = "Have switched logging to '" +
138                     m_switchTo + "'";
139             m_logger.debug( message );
140         }
141     }
142 }
143
Popular Tags