KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > logging > LoggerFactoryImpl


1 package org.apache.ojb.broker.util.logging;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.ojb.broker.util.ClassHelper;
22 import org.apache.commons.lang.SystemUtils;
23
24 /**
25  * The factory class <code>LoggerFactory</code> can be used
26  * to create <code>Logger</code> instances.
27  * The <code>Logger</code>-implementation class served
28  * by the factory is configured by settings in the
29  * OJB.properties file.
30  *
31  * @author Thomas Mahler
32  * @author <a HREF="leandro@ibnetwork.com.br">Leandro Rodrigo Saad Cruz</a>
33  * @version $Id: LoggerFactoryImpl.java,v 1.18.2.6 2005/12/21 22:28:16 tomdz Exp $
34  * @see <a HREF="http://jakarta.apache.org/log4j/docs/index.html">jakarta-log4j</a>
35  */

36 public class LoggerFactoryImpl
37 {
38     public static final String JavaDoc BOOT_LOG_LEVEL_STR = "OJB.bootLogLevel";
39     protected static final String JavaDoc BOOT_STR = "BOOT";
40     protected static final String JavaDoc DEFAULT_STR = "DEFAULT";
41     protected static final LoggerFactoryImpl INSTANCE = new LoggerFactoryImpl();
42
43     private Logger defaultLogger = null;
44
45     private Logger bootLogger = null;
46
47     private boolean bootLoggerIsReassigned = false;
48
49     /** Used for caching logger instances */
50     private Map JavaDoc cache = new HashMap JavaDoc();
51     /** The configuration */
52     private LoggingConfiguration conf;
53
54     // yes. it's a singleton !
55
private LoggerFactoryImpl()
56     {
57     }
58
59     public static LoggerFactoryImpl getInstance()
60     {
61         return INSTANCE;
62     }
63
64     private LoggingConfiguration getConfiguration()
65     {
66         if(conf == null)
67         {
68             // this will load the configuration
69
conf = new LoggingConfiguration();
70         }
71         return conf;
72     }
73
74     /**
75      * returns a minimal logger that needs no configuration
76      * and can thus be safely used during OJB boot phase
77      * (i.e. when OJB.properties have not been loaded).
78      *
79      * @return Logger the OJB BootLogger
80      */

81     public Logger getBootLogger()
82     {
83         if(bootLogger == null)
84         {
85             // create a StringBuffer based Logger for boot log operations
86
bootLogger = createStringBufferLogger_Boot();
87         }
88         return bootLogger;
89     }
90
91
92     /**
93      * returns the default logger. This Logger can
94      * be used when it is not appropriate to use a
95      * dedicated fresh Logger instance.
96      *
97      * @return default Logger
98      */

99     public Logger getDefaultLogger()
100     {
101         if(defaultLogger == null)
102         {
103             defaultLogger = getLogger(DEFAULT_STR);
104         }
105         return defaultLogger;
106     }
107
108
109     /**
110      * returns a Logger. The Logger is named
111      * after the full qualified name of input parameter clazz
112      *
113      * @param clazz the Class which name is to be used as name
114      * @return Logger the returned Logger
115      */

116     public Logger getLogger(Class JavaDoc clazz)
117     {
118         return getLogger(clazz.getName());
119     }
120
121
122     /**
123      * returns a Logger.
124      *
125      * @param loggerName the name of the Logger
126      * @return Logger the returned Logger
127      */

128     public Logger getLogger(String JavaDoc loggerName)
129     {
130         Logger logger;
131         //lookup in the cache first
132
logger = (Logger) cache.get(loggerName);
133
134         if(logger == null)
135         {
136             try
137             {
138                 // get the configuration (not from the configurator because this is independent)
139
logger = createLoggerInstance(loggerName);
140                 if(getBootLogger().isDebugEnabled())
141                 {
142                     getBootLogger().debug("Using logger class '"
143                             + (getConfiguration() != null ? getConfiguration().getLoggerClass() : null)
144                             + "' for " + loggerName);
145                 }
146                 // configure the logger
147
getBootLogger().debug("Initializing logger instance " + loggerName);
148                 logger.configure(conf);
149             }
150             catch(Throwable JavaDoc t)
151             {
152                 // do reassign check and signal logger creation failure
153
reassignBootLogger(true);
154                 logger = getBootLogger();
155                 getBootLogger().error("[" + this.getClass().getName()
156                             + "] Could not initialize logger " + (conf != null ? conf.getLoggerClass() : null), t);
157             }
158             //cache it so we can get it faster the next time
159
cache.put(loggerName, logger);
160             // do reassign check
161
reassignBootLogger(false);
162         }
163         return logger;
164     }
165
166     /**
167      * Creates a new Logger instance for the specified name.
168      */

169     private Logger createLoggerInstance(String JavaDoc loggerName) throws Exception JavaDoc
170     {
171         Class JavaDoc loggerClass = getConfiguration().getLoggerClass();
172         Logger log = (Logger) ClassHelper.newInstance(loggerClass, String JavaDoc.class, loggerName);
173         log.configure(getConfiguration());
174         return log;
175     }
176
177     /**
178      *
179      * @param forceError
180      */

181     protected synchronized void reassignBootLogger(boolean forceError)
182     {
183         // if the boot logger was already reassigned do nothing
184
if(!bootLoggerIsReassigned)
185         {
186             Logger newBootLogger = null;
187             String JavaDoc name = getBootLogger().getName();
188             try
189             {
190                 // 1. try to use a Logger instance based on the configuration files
191
newBootLogger = createLoggerInstance(name);
192             }
193             catch(Exception JavaDoc e) {/*ignore*/}
194             if(newBootLogger == null)
195             {
196                 // 2. if no logging library can be found, use OJB's console logger
197
newBootLogger = createPoorMansLogger_Boot();
198             }
199             if(getBootLogger() instanceof StringBufferLoggerImpl)
200             {
201                 /*
202                 if the StringBuffer based Logger was used for OJB bootstrap process
203                 get the logging statement string and log it on the "real" Logger instance
204                 */

205                 StringBufferLoggerImpl strLogger = (StringBufferLoggerImpl) getBootLogger();
206                 String JavaDoc bootMessage = strLogger.flushLogBuffer();
207                 String JavaDoc eol = SystemUtils.LINE_SEPARATOR;
208                 if(forceError || strLogger.isErrorLog())
209                 {
210                     newBootLogger.error("-- boot log messages -->" + eol + bootMessage);
211                 }
212                 else
213                 {
214                     newBootLogger.info("-- boot log messages -->" + eol + bootMessage);
215                 }
216             }
217             bootLogger = newBootLogger;
218             bootLoggerIsReassigned = true;
219         }
220     }
221
222     protected Logger createPoorMansLogger_Boot()
223     {
224         Logger bootLogger = new PoorMansLoggerImpl(BOOT_STR);
225         // allow user to set boot log level via system property
226
String JavaDoc level = System.getProperty(BOOT_LOG_LEVEL_STR, LoggingConfiguration.OJB_DEFAULT_BOOT_LOG_LEVEL);
227         ((PoorMansLoggerImpl) bootLogger).setLevel(level);
228         return bootLogger;
229     }
230
231     protected Logger createStringBufferLogger_Boot()
232     {
233         Logger bootLogger = new StringBufferLoggerImpl(BOOT_STR);
234         // allow user to set boot log level via system property
235
String JavaDoc level = System.getProperty(BOOT_LOG_LEVEL_STR, LoggingConfiguration.OJB_DEFAULT_BOOT_LOG_LEVEL);
236         ((PoorMansLoggerImpl) bootLogger).setLevel(level);
237         return bootLogger;
238     }
239 }
240
Popular Tags