001: /*
002: This software is OSI Certified Open Source Software.
003: OSI Certified is a certification mark of the Open Source Initiative.
004:
005: The license (Mozilla version 1.0) can be read at the MMBase site.
006: See http://www.MMBase.org/license
007:
008: */
009:
010: package org.mmbase.util.logging.java;
011:
012: import org.mmbase.util.logging.Logger;
013: import org.mmbase.util.logging.Level;
014: import org.mmbase.util.logging.Logging;
015:
016: import java.io.*;
017:
018: import org.mmbase.util.ResourceWatcher;
019: import org.mmbase.util.ResourceLoader;
020:
021: /**
022: * Since java 1.4 there is a Logger implemented in java itself; this MMBase Logger implementation
023: * delegates all logging to this java framework. The java framework is comparable to log4j, so you could use it as an alternative.
024: *
025: <table>
026: <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>
027: <tr><td>TRACE</td><td>{@link java.util.logging.Level#FINEST}</td></tr>
028: <tr><td>TRACE</td><td>{@link java.util.logging.Level#FINER}</td></tr>
029: <tr><td>DEBUG</td><td>{@link java.util.logging.Level#FINE}</td></tr>
030: <tr><td>SERVICE</td><td>{@link java.util.logging.Level#CONFIG}</td></tr>
031: <tr><td>INFO</td><td>{@link java.util.logging.Level#INFO}</td></tr>
032: <tr><td>WARN</td><td>{@link java.util.logging.Level#WARNING}</td></tr>
033: <tr><td>ERROR</td><td>{@link java.util.logging.Level#SEVERE}</td></tr>
034: <tr><td>FATAL</td><td>{@link java.util.logging.Level#SEVERE}</td></tr>
035: </table>
036: *
037: * @author Michiel Meeuwissen
038: * @since MMBase-1.8
039: * @see org.mmbase.util.logging.java.MMBaseLogger
040: */
041:
042: public final class Impl implements Logger {
043:
044: private static Logger log = Logging.getLoggerInstance(Impl.class);
045: private static ResourceWatcher configWatcher;
046:
047: private java.util.logging.Logger logger;
048:
049: /**
050: * Constructor. This calls java's {@link java.util.logging.Logger#getLogger(String)}.
051: */
052:
053: protected Impl(String name) {
054: logger = java.util.logging.Logger.getLogger(name);
055: }
056:
057: /**
058: * Return a MMBase logger object, which wrappes a java.util.logging.Logger object.
059: */
060: public static Impl getLoggerInstance(String name) {
061: return new Impl(name);
062: }
063:
064: /**
065: * Calls LogManager#readConfiguration, and feeds it with the configured InputStream. So you can
066: * configure java-logging. The file is watched, so you can add and change it later.
067: *
068: * There need not be a configuration file for java logging.
069: **/
070:
071: public static void configure(String s) {
072: if (s.equals("")) {
073: System.out
074: .println("Using default java logging configuration");
075: } else {
076: try {
077: log.info("logging configurationfile : " + s);
078:
079: ResourceLoader rl = Logging.getResourceLoader();
080:
081: log.info("using " + rl + " for resolving " + s);
082: configWatcher = new ResourceWatcher(rl) {
083: public void onChange(String s) {
084: try {
085: log.info("Reading configuration file : "
086: + s);
087: java.util.logging.LogManager
088: .getLogManager()
089: .readConfiguration(
090: resourceLoader
091: .getResourceAsStream(s));
092: } catch (IOException ioe) {
093: log.error(ioe);
094: }
095: }
096: };
097:
098: configWatcher.add(s);
099: configWatcher.start();
100:
101: java.util.logging.LogManager.getLogManager()
102: .readConfiguration(rl.getResourceAsStream(s));
103: } catch (IOException ioe) {
104: log.error(ioe);
105: }
106: }
107: }
108:
109: // javadoc inherited
110: public void setLevel(Level p) {
111: switch (p.toInt()) {
112: case Level.TRACE_INT:
113: logger.setLevel(java.util.logging.Level.FINER);
114: break;
115: case Level.DEBUG_INT:
116: logger.setLevel(java.util.logging.Level.FINE);
117: break;
118: case Level.SERVICE_INT:
119: logger.setLevel(java.util.logging.Level.CONFIG);
120: break;
121: case Level.INFO_INT:
122: logger.setLevel(java.util.logging.Level.INFO);
123: break;
124: case Level.WARN_INT:
125: logger.setLevel(java.util.logging.Level.WARNING);
126: break;
127: case Level.ERROR_INT:
128: logger.setLevel(java.util.logging.Level.SEVERE);
129: break;
130: case Level.FATAL_INT:
131: logger.setLevel(java.util.logging.Level.SEVERE);
132: break;
133: }
134:
135: }
136:
137: public void trace(Object m) {
138: logger.log(java.util.logging.Level.FINER, "" + m);
139: }
140:
141: public void trace(Object m, Throwable t) {
142: logger.log(java.util.logging.Level.FINER, "" + m, t);
143: }
144:
145: public void debug(Object m) {
146: logger.log(java.util.logging.Level.FINE, "" + m);
147: }
148:
149: public void debug(Object m, Throwable t) {
150: logger.log(java.util.logging.Level.FINE, "" + m, t);
151: }
152:
153: public void service(Object m) {
154: logger.log(java.util.logging.Level.CONFIG, "" + m);
155: }
156:
157: public void service(Object m, Throwable t) {
158: logger.log(java.util.logging.Level.CONFIG, "" + m, t);
159: }
160:
161: public void info(Object m) {
162: logger.log(java.util.logging.Level.INFO, "" + m);
163: }
164:
165: public void info(Object m, Throwable t) {
166: logger.log(java.util.logging.Level.INFO, "" + m, t);
167: }
168:
169: public void warn(Object m) {
170: logger.log(java.util.logging.Level.WARNING, "" + m);
171: }
172:
173: public void warn(Object m, Throwable t) {
174: logger.log(java.util.logging.Level.WARNING, "" + m, t);
175: }
176:
177: public void error(Object m) {
178: logger.log(java.util.logging.Level.SEVERE, "" + m);
179: }
180:
181: public void error(Object m, Throwable t) {
182: logger.log(java.util.logging.Level.SEVERE, "" + m, t);
183: }
184:
185: public void fatal(Object m) {
186: logger.log(java.util.logging.Level.SEVERE, "" + m);
187: }
188:
189: public void fatal(Object m, Throwable t) {
190: logger.log(java.util.logging.Level.SEVERE, "" + m, t);
191: }
192:
193: private final java.util.logging.Level getLevel() {
194: java.util.logging.Level level = null;
195: java.util.logging.Logger log = logger;
196: while (level == null) {
197: level = log.getLevel();
198: log = log.getParent();
199: }
200: return level;
201: }
202:
203: public boolean isTraceEnabled() {
204: return (getLevel().intValue() <= java.util.logging.Level.FINER
205: .intValue());
206: }
207:
208: public boolean isDebugEnabled() {
209: return (getLevel().intValue() <= java.util.logging.Level.FINE
210: .intValue());
211: }
212:
213: public boolean isServiceEnabled() {
214: return (getLevel().intValue() <= java.util.logging.Level.CONFIG
215: .intValue());
216: }
217:
218: }
|