KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > logging > java > Impl


1 /*
2 This software is OSI Certified Open Source Software.
3 OSI Certified is a certification mark of the Open Source Initiative.
4
5 The license (Mozilla version 1.0) can be read at the MMBase site.
6 See http://www.MMBase.org/license
7
8 */

9
10 package org.mmbase.util.logging.java;
11
12 import org.mmbase.util.logging.Logger;
13 import org.mmbase.util.logging.Level;
14 import org.mmbase.util.logging.Logging;
15
16
17 import java.io.*;
18
19 import org.mmbase.util.ResourceWatcher;
20 import org.mmbase.util.ResourceLoader;
21
22 /**
23  * Since java 1.4 there is a Logger implemented in java itself; this MMBase Logger implementation
24  * delegates all logging to this java framework. The java framework is comparable to log4j, so you could use it as an alternative.
25  *
26  <table>
27  <tr><th>Level in MMBase's {@link org.mmbase.util.logging.Level}</th><th>Level in Java's {@link java.util.logging.Level}</th></tr>
28  <tr><td>TRACE</td><td>{@link java.util.logging.Level#FINEST}</td></tr>
29  <tr><td>TRACE</td><td>{@link java.util.logging.Level#FINER}</td></tr>
30  <tr><td>DEBUG</td><td>{@link java.util.logging.Level#FINE}</td></tr>
31  <tr><td>SERVICE</td><td>{@link java.util.logging.Level#CONFIG}</td></tr>
32  <tr><td>INFO</td><td>{@link java.util.logging.Level#INFO}</td></tr>
33  <tr><td>WARN</td><td>{@link java.util.logging.Level#WARNING}</td></tr>
34  <tr><td>ERROR</td><td>{@link java.util.logging.Level#SEVERE}</td></tr>
35  <tr><td>FATAL</td><td>{@link java.util.logging.Level#SEVERE}</td></tr>
36  </table>
37  *
38  * @author Michiel Meeuwissen
39  * @since MMBase-1.8
40  * @see org.mmbase.util.logging.java.MMBaseLogger
41  */

42
43
44 public final class Impl implements Logger {
45
46     private static Logger log = Logging.getLoggerInstance(Impl.class);
47     private static ResourceWatcher configWatcher;
48
49     private java.util.logging.Logger JavaDoc logger;
50
51
52     /**
53      * Constructor. This calls java's {@link java.util.logging.Logger#getLogger(String)}.
54      */

55
56     protected Impl(String JavaDoc name) {
57         logger = java.util.logging.Logger.getLogger(name);
58     }
59
60
61
62     /**
63      * Return a MMBase logger object, which wrappes a java.util.logging.Logger object.
64      */

65     public static Impl getLoggerInstance(String JavaDoc name) {
66         return new Impl(name);
67     }
68
69
70     /**
71      * Calls LogManager#readConfiguration, and feeds it with the configured InputStream. So you can
72      * configure java-logging. The file is watched, so you can add and change it later.
73      *
74      * There need not be a configuration file for java logging.
75      **/

76
77     public static void configure(String JavaDoc s) {
78         if (s.equals("")) {
79             System.out.println("Using default java logging configuration");
80         } else {
81             try {
82                 log.info("logging configurationfile : " + s);
83
84                 ResourceLoader rl = Logging.getResourceLoader();
85
86                 log.info("using " + rl + " for resolving " + s);
87                 configWatcher = new ResourceWatcher (rl) {
88                         public void onChange(String JavaDoc s) {
89                             try {
90                                 log.info("Reading configuration file : " + s);
91                                 java.util.logging.LogManager.getLogManager().readConfiguration(resourceLoader.getResourceAsStream(s));
92                             } catch (IOException ioe) {
93                                 log.error(ioe);
94                             }
95                         }
96                     };
97
98                 configWatcher.add(s);
99                 configWatcher.start();
100
101                 java.util.logging.LogManager.getLogManager().readConfiguration(rl.getResourceAsStream(s));
102             } catch (IOException ioe) {
103                 log.error(ioe);
104             }
105         }
106     }
107
108     // javadoc inherited
109
public void setLevel(Level p) {
110         switch (p.toInt()) {
111         case Level.TRACE_INT: logger.setLevel(java.util.logging.Level.FINER); break;
112         case Level.DEBUG_INT: logger.setLevel(java.util.logging.Level.FINE); break;
113         case Level.SERVICE_INT: logger.setLevel(java.util.logging.Level.CONFIG); break;
114         case Level.INFO_INT: logger.setLevel(java.util.logging.Level.INFO); break;
115         case Level.WARN_INT: logger.setLevel(java.util.logging.Level.WARNING); break;
116         case Level.ERROR_INT: logger.setLevel(java.util.logging.Level.SEVERE); break;
117         case Level.FATAL_INT: logger.setLevel(java.util.logging.Level.SEVERE); break;
118         }
119
120     }
121
122
123     public void trace (Object JavaDoc m) {
124         logger.log(java.util.logging.Level.FINER, "" + m);
125     }
126     public void trace (Object JavaDoc m, Throwable JavaDoc t) {
127         logger.log(java.util.logging.Level.FINER, "" + m, t);
128     }
129     public void debug (Object JavaDoc m) {
130         logger.log(java.util.logging.Level.FINE, "" + m);
131     }
132     public void debug (Object JavaDoc m, Throwable JavaDoc t) {
133         logger.log(java.util.logging.Level.FINE, "" + m, t);
134     }
135
136     public void service (Object JavaDoc m) {
137         logger.log(java.util.logging.Level.CONFIG, "" + m);
138     }
139     public void service (Object JavaDoc m, Throwable JavaDoc t) {
140         logger.log(java.util.logging.Level.CONFIG, "" + m, t);
141     }
142     public void info (Object JavaDoc m) {
143         logger.log(java.util.logging.Level.INFO, "" + m);
144     }
145     public void info (Object JavaDoc m, Throwable JavaDoc t) {
146         logger.log(java.util.logging.Level.INFO, "" + m, t);
147     }
148     public void warn (Object JavaDoc m) {
149         logger.log(java.util.logging.Level.WARNING, "" + m);
150     }
151     public void warn (Object JavaDoc m, Throwable JavaDoc t) {
152         logger.log(java.util.logging.Level.WARNING, "" + m, t);
153     }
154     public void error (Object JavaDoc m) {
155         logger.log(java.util.logging.Level.SEVERE, "" + m);
156     }
157     public void error (Object JavaDoc m, Throwable JavaDoc t) {
158         logger.log(java.util.logging.Level.SEVERE, "" + m, t);
159     }
160     public void fatal (Object JavaDoc m) {
161         logger.log(java.util.logging.Level.SEVERE, "" + m);
162     }
163     public void fatal (Object JavaDoc m, Throwable JavaDoc t) {
164         logger.log(java.util.logging.Level.SEVERE, "" + m, t);
165     }
166
167     private final java.util.logging.Level JavaDoc getLevel() {
168         java.util.logging.Level JavaDoc level = null;
169         java.util.logging.Logger JavaDoc log = logger;
170         while (level == null) {
171             level = log.getLevel();
172             log = log.getParent();
173         }
174         return level;
175     }
176
177     public boolean isTraceEnabled() {
178         return (getLevel().intValue() <= java.util.logging.Level.FINER.intValue());
179     }
180     
181     public boolean isDebugEnabled() {
182         return (getLevel().intValue() <= java.util.logging.Level.FINE.intValue());
183     }
184
185     public boolean isServiceEnabled() {
186         return (getLevel().intValue() <= java.util.logging.Level.CONFIG.intValue());
187     }
188
189 }
190
191
Popular Tags