KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > logging > ModFactLogger


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

20 package org.objectweb.modfact.corba.logging;
21
22 import java.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.objectweb.util.monolog.Monolog;
28 import org.objectweb.util.monolog.api.Logger;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.util.monolog.api.LoggerFactory;
31
32 /**
33  * This class represents the Logger used by the ModFact project.
34  * It delegates the log process to the log framework, thus only
35  * this class would be modified if the log framework changed.
36  *
37  * @author Pierre Carpentier
38  */

39 public class ModFactLogger {
40
41     /** The delegates logger. */
42     private Logger logger;
43
44     /** The factory of logger. */
45     private static LoggerFactory loggerFactory = getLoggerFactory();
46     
47     /** The quiet flag indicates if the logger is in quiet or in normal mode. */
48     private static boolean isQuiet = false;
49     
50     /** The default file name of the properties file. */
51     private static String JavaDoc configFile;
52
53     /**
54      * Constructor of a named logger.
55      * @param name The name of the logger.
56      */

57     public ModFactLogger(String JavaDoc name) {
58         if (loggerFactory != null)
59             logger = loggerFactory.getLogger(name);
60     }
61
62     /**
63      * Logs a message with the appropriate level.
64      * @param level The level of logging.
65      * @param message The message to log.
66      */

67     public void log(int level, String JavaDoc message) {
68         if (logger != null && !isQuiet) {
69             switch (level) {
70                 case Level.FATAL :
71                     logger.log(BasicLevel.FATAL, message);
72                     break;
73                 case Level.ERROR :
74                     logger.log(BasicLevel.ERROR, message);
75                     break;
76                 case Level.WARN :
77                     logger.log(BasicLevel.WARN, message);
78                     break;
79                 case Level.INFO :
80                     logger.log(BasicLevel.INFO, message);
81                     break;
82                 case Level.FINE :
83                     logger.log(BasicLevel.DEBUG, message);
84                     break;
85                 case Level.FINER :
86                     logger.log(BasicLevel.INHERIT, message);
87                     break;
88             }
89         }
90     }
91
92     /**
93      * Gets a logger from its name (or creates it if the name is unknown).
94      * @param name The name of the logger.
95      * @return The logger.
96      */

97     public static ModFactLogger getLogger(String JavaDoc name) {
98         return new ModFactLogger(name);
99     }
100     
101     /**
102      * Initializes the ModFactLogger.
103      */

104     public static void init() {
105         configFile = System.getProperty(Monolog.MONOLOG_FILE_NAME, Monolog.DEFAULT_MONOLOG_FILE);
106         Monolog.init();
107     }
108
109     /**
110      * Gets the logger factory defined by the class name
111      * set in the property log.config.classname. <br>
112      * This variable can be set to :
113      * - org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory
114      * for use the javaLog service. <br>
115      * - org.objectweb.util.monolog.wrapper.log4j.MonologLoggerFactory
116      * for use the log4j library.
117      * @return The factory of logger.
118      */

119     public static LoggerFactory getLoggerFactory() {
120         init();
121         // load properties from config file
122
Properties JavaDoc props = new Properties JavaDoc();
123         String JavaDoc loggerFactoryName = null;
124         try {
125             props.load(new java.io.FileInputStream JavaDoc(configFile));
126             loggerFactoryName = props.getProperty("log.config.classname", null);
127         } catch (IOException JavaDoc ioe) {
128             // Maybe Web Application
129
try {
130                 String JavaDoc url = System.getProperty("org.objectweb.modfact.resourceURL")+"/"+configFile;
131             URL JavaDoc webPath = new URL JavaDoc(url);
132             props.load(webPath.openStream());
133             loggerFactoryName = props.getProperty("log.config.classname", null);
134             }
135             catch (MalformedURLException JavaDoc ex) {
136             }
137             catch (IOException JavaDoc ex) {
138             }
139         }
140         
141         if (loggerFactoryName == null) {
142             loggerFactoryName = System.getProperty("log.config.classname",
143                                 "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory");
144         }
145         try {
146             LoggerFactory lf = (LoggerFactory) Class.forName(loggerFactoryName).newInstance();
147             return lf;
148         } catch (Exception JavaDoc e) {
149             System.err.println("The logger factory class name is not correct.");
150             return null;
151         }
152     }
153     
154     /**
155      * Sets the quiet flag.
156      * TRUE to sets the logger in a quiet mode
157      * FALSE to sets the logger in a normal mode.
158      */

159     public static void setQuiet (boolean quietMode) {
160         isQuiet = quietMode;
161     }
162
163 }
164
Popular Tags