KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > javaLog > LoggerFactory


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
19 package org.objectweb.util.monolog.wrapper.javaLog;
20
21 import org.objectweb.util.monolog.api.BasicLevel;
22 import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
23 import org.objectweb.util.monolog.wrapper.javaLog.Logger;
24 import org.objectweb.util.monolog.Monolog;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.logging.LogManager JavaDoc;
30
31 /**
32  * is the LoggerFactory for the wrapper to java.util.logging
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class LoggerFactory extends AbstractFactory {
37
38     /**
39      * the LogManager of java.util.logging
40      */

41     protected static LogManager JavaDoc manager = null;
42
43     /**
44      * the root logger
45      */

46     protected static Logger rootLogger = null;
47
48
49     /**
50      * Keep a reference to loggers.
51      * @see java.util.logging.LogManager addLogger javadoc
52      */

53     private ArrayList JavaDoc loggers = new ArrayList JavaDoc();
54
55     /**
56      * This static code initialize the value of the variable defined into
57      * BasicLevel.
58      */

59     static {
60         BasicLevel.INHERIT = -1;
61         debug("INHERIT= " + BasicLevel.INHERIT);
62         BasicLevel.DEBUG = java.util.logging.Level.FINEST.intValue();
63         debug("DEBUG= " + BasicLevel.DEBUG);
64         BasicLevel.INFO = java.util.logging.Level.INFO.intValue();
65         debug("INFO= " + BasicLevel.INFO);
66         BasicLevel.WARN = java.util.logging.Level.WARNING.intValue();
67         debug("WARN= " + BasicLevel.WARN);
68         BasicLevel.ERROR = java.util.logging.Level.SEVERE.intValue();
69         debug("ERROR= " + BasicLevel.ERROR);
70         BasicLevel.FATAL = java.util.logging.Level.SEVERE.intValue();
71         debug("FATAL= " + BasicLevel.FATAL);
72
73         BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT);
74         BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG);
75         BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO);
76         BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN);
77         BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR);
78         BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL);
79
80         manager = LogManager.getLogManager();
81         if (classLoaderIsoltion) {
82             int i = 0;
83             while (rootLogger == null) {
84                 rootLoggerName = "root" + i;
85                 synchronized (manager) {
86                     // the manager object is shared between the multiple instances of this class
87
if (manager.getLogger(rootLoggerName) == null) {
88                         rootLogger = new Logger(rootLoggerName, null);
89                         manager.addLogger(rootLogger);
90                     } else {
91                         i ++;
92                     }
93                 }
94             }
95             rootLogger.setUseParentHandlers(false);
96             Monolog.debug("Instanciate the root logger " + rootLoggerName);
97             rootLoggerPrefix = rootLoggerName + '.';
98         } else {
99             rootLogger = new Logger(manager.getLogger(""));
100         }
101     }
102
103     /**
104      * This constant is used to initialize the factory with the configure method
105      */

106     public final static String JavaDoc JAVALOG_CONFIGURATION = "javaLogConfiguration";
107
108     /**
109      * This constant means that this java log system must be initialize with
110      * the default configuration
111      */

112     public final static String JavaDoc DEFAULT = "default";
113
114     /**
115      * This constant means that this java log system must be initialize with
116      * a property file
117      */

118     public final static String JavaDoc PROPERTY = "property";
119
120     /**
121      * This constant means that this java log system must be initialize with
122      * a xml file
123      */

124     public final static String JavaDoc CLASS = "class";
125
126     /**
127      * This constant is the properties file name with wich the java log system
128      * must be initialized.
129      */

130     public final static String JavaDoc JAVALOG_CONFIGURATION_FILE
131             = "javaLogConfigurationFile";
132
133     /**
134      * This constant is the properties class name with wich the java log system
135      * must be initialized.
136      */

137     public final static String JavaDoc JAVALOG_CONFIGURATION_CLASS
138             = "javaLogConfigurationClass";
139
140     public LoggerFactory() {
141         super();
142     }
143
144     public String JavaDoc getWrapperName() {
145         return "javaLog";
146     }
147
148     protected String JavaDoc[][] getDefaultHandlerType2className() {
149         return new String JavaDoc[][] {
150             {handlerTypes[0], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
151             {handlerTypes[1], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
152             {handlerTypes[2], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
153             {handlerTypes[3], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
154             {handlerTypes[4], "org.objectweb.util.monolog.wrapper.javaLog.JMXGenericHandler"}
155         };
156     }
157
158     /**
159      * This method allocates org.objectweb.util.monolog.wrapper.javaLog.Logger
160      * objects whic are also java.util.logging.Logger and
161      * org.objectweb.util.monolog.api.Logger.
162      */

163     protected synchronized Logger getMonoLogger(String JavaDoc name, String JavaDoc resName) {
164         if (name == null)
165             throw new IllegalArgumentException JavaDoc(
166                     "The specified Logger name is null");
167         if (name.equals("root") || name.length()==0) {
168             return rootLogger;
169         }
170         // isolates the logger hierarchy
171
name = monoLoggerName(name);
172         // Search if the logger already exist.
173
Object JavaDoc o = manager.getLogger(name);
174         if (o == null) {
175             // It doesn't exist => creates and adds it
176
Logger result = new Logger(name, resName);
177             manager.addLogger(result);
178             Monolog.debug("Instanciate the logger " + name);
179
180             // In the javadoc of the LogManager, it is said that the caller
181
// need to keep a reference to this logger to avoid GC.
182
// Because LogManager could use some weak reference on it
183
loggers.add(result);
184             return result;
185         } else if (!o.getClass().equals(Logger.class)) {
186             throw new RuntimeException JavaDoc("Bad logger class (logger name: '"
187                     + name +"'), expected: " + Logger.class
188                     + ", found: " + o.getClass());
189         } else {
190             return (Logger) o;
191         }
192     }
193
194
195     // IMPLEMENTATION OF THE Configurable INTERFACE //
196
//----------------------------------------------//
197

198     /**
199      * This method permits to configure the factory.
200      * The properties parameter must contains the JAVALOG_CONFIGURATION property.
201      * Its value can be DEFAULT, PROPERTY or XML.
202      * In the PROPERTY case of the properties parameter must also contain the
203      * JAVALOG_CONFIGURATION_FILE property which the value is the configuration
204      * file name.
205      * In the CLASS case of the properties parameter must also contain the
206      * JAVALOG_CONFIGURATION_CLASS property which the value is the configuration
207      * class name which will initialized the java log system..
208      *
209      */

210     public void configure(Properties JavaDoc prop) throws Exception JavaDoc {
211         if (prop == null)
212             return;
213         String JavaDoc confMode = prop.getProperty(JAVALOG_CONFIGURATION, null);
214         if (confMode == null)
215             return;
216
217         String JavaDoc param = null;
218         if (confMode.equals(PROPERTY)) {
219             param = prop.getProperty(JAVALOG_CONFIGURATION_FILE, null);
220             if (param != null)
221                 System.setProperty("java.util.logging.config.file", param);
222             manager.readConfiguration();
223         } else if (confMode.equals(CLASS)) {
224             param = prop.getProperty(JAVALOG_CONFIGURATION_CLASS, null);
225             if (param != null)
226                 System.setProperty("java.util.logging.config.class", param);
227             manager.readConfiguration();
228         }
229     }
230
231
232     // IMPLEMENTATION OF LoggerFactory INTERFACE //
233
//-------------------------------------------//
234

235     public org.objectweb.util.monolog.api.Logger getLogger(String JavaDoc key) {
236         return getMonoLogger(key, resourceBundleName);
237     }
238
239     public org.objectweb.util.monolog.api.Logger getLogger(String JavaDoc key, String JavaDoc rbn) {
240         return getMonoLogger(key, rbn);
241     }
242
243     /**
244      * It retrieves a list of all loggers.
245      */

246     public org.objectweb.util.monolog.api.Logger[] getLoggers() {
247         ArrayList JavaDoc res = new ArrayList JavaDoc();
248         for (Enumeration JavaDoc e = manager.getLoggerNames(); e.hasMoreElements();) {
249              Object JavaDoc o = manager.getLogger((String JavaDoc) e.nextElement());
250              if (o instanceof Logger) {
251                  res.add(o);
252              }
253         }
254          return (Logger[]) res.toArray(new Logger[0]);
255      }
256 }
257
Popular Tags