KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > Monolog


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog;
19
20 import org.objectweb.util.monolog.api.LoggerFactory;
21 import org.objectweb.util.monolog.api.HandlerFactory;
22 import org.objectweb.util.monolog.api.LevelFactory;
23 import org.objectweb.util.monolog.api.MonologFactory;
24 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
25 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
26
27 import java.util.Properties JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33
34 /**
35  * This class is a helper to instanciate a logger factory. It provides a set of
36  * init(...) method with various parameter:
37  * - no parameter => automatic configuration with system property and default
38  * values
39  * - Properties => contains monolog configuration (loggers, handlers, levels
40  * and optionaly the wrapper class name (automatic choice if not specified)).
41  * - String => the class name of the wrapper class to instanciate
42  *
43  * @author S.Chassande-Barrioz
44  */

45 public class Monolog {
46     /**
47      * Inidicates if the monolog wrapper must be logged itself.
48      */

49     public static boolean debug = new Boolean JavaDoc(
50             System.getProperty("monolog.debug")).booleanValue();
51
52     /**
53      * is the basic loggerFactory implementation.
54      */

55     private static MonologFactory basicMonologFactory = new LoggerImpl();
56
57     /**
58      * is the last required logger factory. By default it is initialized to the
59      * a basic logger factory implementation (LoggerImpl).
60      */

61     public static MonologFactory monologFactory = basicMonologFactory;
62
63     /**
64      * @deprecated use monologFactory
65      */

66     public static LoggerFactory loggerFactory = monologFactory;
67
68     /**
69      * is the default monolog configuration file lookup from the file system or
70      * from the classpath.
71      */

72     public static final String JavaDoc DEFAULT_MONOLOG_FILE = "monolog.properties";
73
74     /**
75      * is the property name of the monolog configuration file. This property
76      * is searched from the specified properties or from the system properties.
77      */

78     public static final String JavaDoc MONOLOG_FILE_NAME = "monolog.filename";
79
80     /**
81      * is the property name of the wrapper class name. This property is searched
82      * from the specified properties or from the system properties.
83      */

84     public static final String JavaDoc MONOLOG_CLASS_NAME = "monolog.classname";
85
86     /**
87      * is the class name of logger factory for the log4j logging system.
88      */

89     public static final String JavaDoc LOG4J_WRAPPER_CLASS_NAME
90             = "org.objectweb.util.monolog.wrapper.log4j.MonologLoggerFactory";
91
92     /**
93      * is the class name of logger factory for the log4jME logging system.
94      */

95     public static final String JavaDoc LOG4JMini_WRAPPER_CLASS_NAME
96             = "org.objectweb.util.monolog.wrapper.log4jMini.MonologLoggerFactory";
97
98     /**
99      * is the class name of logger factory for the java.util.logging logging
100      * system.
101      */

102     public static final String JavaDoc JDK_WRAPPER_CLASS_NAME
103             = "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory";
104     /**
105      * is the class name used to known if log4j is availlable in the classpath
106      */

107     private static final String JavaDoc LOG4J_CLASS_NAME = "org.apache.log4j.Logger";
108
109     /**
110      * is the class name used to known if log4j is availlable in the classpath
111      */

112     private static final String JavaDoc LOG4JMini_CLASS_NAME = "org.apache.log4j.Category";
113
114     /**
115      * is the class name used to known if the JVM version is greater or equal
116      * to 1.4
117      */

118     private static final String JavaDoc JDK_CLASS_NAME = "java.util.logging.Logger";
119
120
121     public static MonologFactory getMonologFactory() {
122         return monologFactory;
123     }
124
125     public static MonologFactory getDefaultMonologFactory() {
126         return basicMonologFactory;
127     }
128
129     /**
130      * Initializes Monolog.
131      * It searches in system properties a monolog configuration file
132      * (MONOLOG_FILE_NAME system property) or use the default file name
133      * (DEFAULT_MONOLOG_FILE system property).
134      *
135      * @return the loggerFactory (never null)
136      */

137     public static MonologFactory initialize() {
138         if (monologFactory == basicMonologFactory) {
139             getMonologFactory(System.getProperty(MONOLOG_FILE_NAME, DEFAULT_MONOLOG_FILE));
140         }
141         return monologFactory;
142     }
143
144     /**
145      * @deprecated use initialize()
146      */

147     public static LoggerFactory init() {
148         return initialize();
149     }
150
151     /**
152      * @deprecated use getMonologFactory(String)
153      */

154     public static LoggerFactory init(String JavaDoc fileName) {
155         return getMonologFactory(fileName);
156     }
157
158     /**
159      * Initializes Monolog with a given monolog configuration file name
160      * if the file is found, it delegates the intialization to init(Properties)
161      * method. if the file is not found it delegates the logger factory
162      * instanciation to the getLoggerFactory(String) method.
163      *
164      * @param fileName is the file name of a properties reachable from the
165      * file system or the classpath.
166      * @return the loggerFactory (never null)
167      */

168     public static MonologFactory getMonologFactory(String JavaDoc fileName) {
169         Monolog.debug("DEBUG: debug is activated!");
170         File JavaDoc f = new File JavaDoc(fileName);
171         InputStream JavaDoc is = null;
172         if (f.exists()) {
173             Monolog.debug("DEBUG: The file " + fileName
174                     + " was found from the file system.");
175             try {
176                 is = new FileInputStream JavaDoc(f);
177             } catch (FileNotFoundException JavaDoc e) {
178                 Monolog.debug("ERROR: " + e.getMessage());
179                 is = null;
180             }
181         } else {
182             is = Monolog.class.getClassLoader().getResourceAsStream(fileName);
183             if (is != null)
184                 Monolog.debug("DEBUG: The file " + fileName
185                         + " was found from the classpath.");
186         }
187         if (is != null) {
188             Properties JavaDoc p = new Properties JavaDoc();
189             try {
190                 p.load(is);
191             } catch (IOException JavaDoc e) {
192                 Monolog.debug("ERROR: problem to load properties from " +
193                         "the input stream " + is);
194                 if (debug) {
195                     e.printStackTrace(System.out);
196                 }
197             }
198             getMonologFactory(p);
199         } else {
200             Monolog.debug("DEBUG: The file " + fileName
201                     + " was not found.");
202             monologFactory = instanciateMonologFactory(null);
203         }
204         return monologFactory;
205     }
206
207     /**
208      * Initializes monolog in with a Properties instance.
209      * It searches from the properties the logger factory class name (
210      * MONOLOG_CLASS_NAME property) and it delegates its configuration to
211      * the loadMonologConfiguration(Properties, LoggerFactory) method.
212      *
213      * @param properties can contains the MONOLOG_CLASS_NAME property and the
214      * rest of the monolog configuration (loggers, handlers, levels).
215      * @return the loggerFactory (never null)
216      */

217     public static MonologFactory getMonologFactory(Properties JavaDoc properties) {
218         monologFactory = instanciateMonologFactory(properties.getProperty(
219                 MONOLOG_CLASS_NAME, System.getProperty(MONOLOG_CLASS_NAME)));
220         if (monologFactory != basicMonologFactory) {
221             loadMonologConfiguration(properties, monologFactory);
222         }
223         return monologFactory;
224     }
225
226     /**
227      * @deprecated use getMonologFactory(Properties)
228      */

229     public static LoggerFactory init(Properties JavaDoc properties) {
230         return getMonologFactory(properties);
231     }
232
233     /**
234      * Loads a monolog configuration into the loggerFactory.
235      * @param properties contains the properties to load into the given logger
236      * factory.
237      */

238     public static void loadMonologConfiguration(Properties JavaDoc properties) {
239         loadMonologConfiguration(properties, monologFactory);
240     }
241
242     /**
243      * Loads a monolog configuration into an existing MonologFactory.
244      * @param properties contains the properties to load into the given logger
245      * factory.
246      * @param mf is the MonologFactory to configure.
247      */

248     public static void loadMonologConfiguration(Properties JavaDoc properties, MonologFactory mf) {
249         try {
250             PropertiesConfAccess.load(properties, mf, mf, mf);
251         } catch (Exception JavaDoc e) {
252             Monolog.error("WARN: problem to configure a loggerFactory:", e);
253         }
254     }
255     /**
256      * @deprecated use loadMonologConfiguration(Properties, MonologFactory)
257      */

258     public static void loadMonologConfiguration(Properties JavaDoc properties, LoggerFactory lf) {
259         loadMonologConfiguration(properties, (MonologFactory) lf);
260     }
261
262     public static LoggerFactory getLoggerFactory(String JavaDoc className) {
263         return getMonologFactory(className);
264     }
265
266     public static LevelFactory getLevelFactory(String JavaDoc className) {
267         return getMonologFactory(className);
268     }
269
270     public static HandlerFactory getHandlerFactory(String JavaDoc className) {
271         return getMonologFactory(className);
272     }
273
274     /**
275      * Retrieves a MonologFactory instance.
276      * First it tries to instanciate the given logger factory class name.
277      * If it is not possible it tries to instanciate the logger factory of the
278      * log4j system (if the classes are availlable).
279      * If log4j or its wrapper are not reachable then it tries to instanciate
280      * the wrapper of the log4jME logging system.
281      * If log4jME or its wrapper are not reachable then it tries to instanciate
282      * the wrapper of the java.util.logging logging system.
283      * Finally if any of these wrappers are reachable the basic logger factory
284      * is used.
285      *
286      * @param className is the class name of a LoggerFactory implementation.
287      * It can be a null value.
288      * @return a LoggerFactory instance (never null).
289      */

290     public static MonologFactory instanciateMonologFactory(String JavaDoc className) {
291         //Try with the given logger factory class name if it is not null
292
if (className != null) {
293             try {
294                 Monolog.debug("DEBUG: try to use the logger factory: " + className);
295                 return (MonologFactory) Class.forName(className).newInstance();
296             } catch (ClassNotFoundException JavaDoc e) {
297                 Monolog.debug("WARN: The class " + className
298                         + " was not found.");
299             } catch (Exception JavaDoc e) {
300                 Monolog.debug("WARN: The class " + className
301                         + " has no public empty constructor.");
302             }
303         }
304         //Try to find the log4j classes and its wrapper
305
try {
306             Monolog.debug("DEBUG: try to use log4j");
307             Class.forName(LOG4J_CLASS_NAME);
308             return (MonologFactory)
309                     Class.forName(LOG4J_WRAPPER_CLASS_NAME).newInstance();
310         } catch (Exception JavaDoc e) {
311             Monolog.debug("DEBUG: log4j and its wrapper are not " +
312                     "availlable:" + e.getMessage());
313             if (debug) {
314                 e.printStackTrace(System.out);
315             }
316         }
317         //Try to find the jdk classes and its wrapper
318
try {
319             Monolog.debug("DEBUG: try to use the jdk");
320             Class.forName(JDK_CLASS_NAME);
321             return (MonologFactory)
322                     Class.forName(JDK_WRAPPER_CLASS_NAME).newInstance();
323         } catch (Exception JavaDoc e) {
324             Monolog.debug("DEBUG: java.util.logging and its wrapper " +
325                     "are not availlable:" + e.getMessage());
326             if (debug) {
327                 e.printStackTrace(System.out);
328             }
329         }
330
331         //Try to find the log4jMini classes and its wrapper
332
try {
333             Monolog.debug("DEBUG: try to use the log4j mini");
334             Class.forName(LOG4JMini_CLASS_NAME);
335             return (MonologFactory)
336                     Class.forName(LOG4JMini_WRAPPER_CLASS_NAME).newInstance();
337         } catch (Exception JavaDoc e) {
338             Monolog.debug("DEBUG: log4jMini and its wrapper are not " +
339                     "availlable:" + e.getMessage());
340             if (debug) {
341                 e.printStackTrace(System.out);
342             }
343         }
344         Monolog.debug("DEBUG: return the basic logger factory");
345         //return the basic logger factory
346
return basicMonologFactory;
347     }
348     /**
349      * This method must be only used to debug the Monolog wrappers.
350      * To active the log of monolog assign the "true" value to the system
351      * property "monolog.debug".
352      *
353      * @param m the message to log.
354      */

355     static public void debug(String JavaDoc m) {
356         if (debug) {
357               System.out.println(m);
358         }
359       }
360     static public void error(String JavaDoc m, Exception JavaDoc e) {
361         System.err.println(m);
362         if (debug) {
363             e.printStackTrace(System.err);
364         }
365       }
366 }
367
Popular Tags