KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > trace > TraceSystem


1 /*====================================================================
2
3 ObjectWeb Util Launcher Package.
4 Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Romain Rouvoy.
23 Contributor(s): .
24
25 --------------------------------------------------------------------
26 $Id: TraceSystem.java,v 1.1 2004/02/13 17:46:08 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.trace;
30
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import org.objectweb.util.monolog.api.HandlerFactory;
37 import org.objectweb.util.monolog.api.LevelFactory;
38 import org.objectweb.util.monolog.api.LoggerFactory;
39 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
40
41
42
43 /**
44  * Specialization of the Monolog trace mechanism into a TraceSystem
45  * for dealing with trace in the scope of the Launcher.<BR>
46  * <p>
47  * The TraceSystem provides a set of preconfigured loggers (through
48  * the default.properties file) for enabling trace in the launcher.
49  * </p>
50  *
51  * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
52  * @version 0.1
53  */

54 public class TraceSystem
55 {
56     /** prefix used to identify Launcher loggers */
57     protected static final String JavaDoc prefix = "org.objectweb.util";
58
59     /** list of current logger instancied */
60     protected static Map JavaDoc list_ = new HashMap JavaDoc();
61
62     /** default level of log */
63     protected static String JavaDoc level_ = "default";
64
65     /**
66      * Configure the log for Launcher
67      * Log configuration is stored in a property file, <code>trace.properties</code>,
68      * which should be available from the classpath.
69      */

70     protected static LoggerFactory getLoggerFactory() {
71         Properties JavaDoc props = new Properties JavaDoc();
72         try {
73             props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(level_+".properties"));
74         } catch (Exception JavaDoc e) {
75             System.err.println("Configuration file for log not found. Traces are disabled: " + e);
76             return null;
77         }
78         
79         // Instanciate the LoggerFactory
80
String JavaDoc lfClassName = props.getProperty("log.config.classname", null);
81         if (lfClassName == null) {
82             System.err.println("Malformed log configuration file: log.config.classname not available");
83         }
84
85         try {
86             LoggerFactory lf = (LoggerFactory) Class.forName(lfClassName).newInstance();
87             // Configure the LoggerFactory with the properties
88
PropertiesConfAccess.load(props, lf, (HandlerFactory) lf, (LevelFactory) lf);
89             return lf;
90         } catch (Exception JavaDoc e) {
91             System.err.println("Logs are disabled:" + e);
92         }
93
94         return null;
95     }
96
97     /**
98      * Creates a new logger identified by logger value
99      *
100      * @param logger - the name of the logger
101      */

102     protected static void create(String JavaDoc logger) {
103         list_.put(logger,
104                   new TraceTemplate(getLoggerFactory().getLogger(prefix+"."+logger)));
105     }
106
107     /**
108      * Changes the level of log
109      *
110      * @param level - the name of the level
111      */

112     public static void setLevel(String JavaDoc level) {
113         level_ = level;
114     }
115
116     /**
117      * Retrieve the instance of a logger
118      *
119      * @param id - the identifier of the logger
120      */

121     public static Trace get(String JavaDoc id) {
122         if (!list_.containsKey(id))
123             create(id);
124         return (Trace) list_.get(id);
125     }
126 }
127
Popular Tags