001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006-2007, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.util.logging;
017:
018: import java.util.logging.Level;
019: import org.apache.commons.logging.Log;
020:
021: /**
022: * An adapter that redirect all Java logging events to the Apache's
023: * <A HREF="http://jakarta.apache.org/commons/logging/">Commons-logging</A> framework.
024: *
025: * @since 2.4
026: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/logging/CommonsLogger.java $
027: * @version $Id: CommonsLogger.java 27891 2007-11-14 14:10:48Z desruisseaux $
028: * @author Martin Desruisseaux
029: * @author Saul Farber
030: *
031: * @see CommonsLoggerFactory
032: * @see Logging
033: */
034: final class CommonsLogger extends LoggerAdapter {
035: /**
036: * The Apache logger to use.
037: */
038: final Log logger;
039:
040: /**
041: * Creates a new logger.
042: *
043: * @param name The logger name.
044: * @param logger The result of {@code LogFactory.getLog(name)}.
045: */
046: public CommonsLogger(final String name, final Log logger) {
047: super (name);
048: this .logger = logger;
049: }
050:
051: /**
052: * Do nothing since Commons-Logging doesn't support programmatic change of logging level.
053: */
054: public void setLevel(Level level) {
055: }
056:
057: /**
058: * Returns the level for this logger.
059: */
060: public Level getLevel() {
061: if (logger.isTraceEnabled())
062: return Level.FINEST;
063: if (logger.isDebugEnabled())
064: return Level.FINE;
065: if (logger.isInfoEnabled())
066: return Level.CONFIG;
067: if (logger.isWarnEnabled())
068: return Level.WARNING;
069: if (logger.isErrorEnabled())
070: return Level.SEVERE;
071: if (logger.isFatalEnabled())
072: return Level.SEVERE;
073: return Level.OFF;
074: }
075:
076: /**
077: * Returns {@code true} if the specified level is loggable.
078: */
079: public boolean isLoggable(final Level level) {
080: final int n = level.intValue();
081: switch (n / 100) {
082: default: {
083: switch (n) { // Special cases (should not occur often).
084: case Integer.MIN_VALUE:
085: return true; // ALL
086: case Integer.MAX_VALUE:
087: return false; // OFF
088: default:
089: return n >= 0 && logger.isFatalEnabled();
090: }
091: }
092: case 10:
093: return logger.isErrorEnabled(); // SEVERE
094: case 9:
095: return logger.isWarnEnabled(); // WARNING
096: case 8: // INFO
097: case 7:
098: return logger.isInfoEnabled(); // CONFIG
099: case 6: // (not allocated)
100: case 5:
101: return logger.isDebugEnabled(); // FINE
102: case 4: // FINER
103: case 3: // FINEST
104: case 2: // (not allocated)
105: case 1: // (not allocated)
106: case 0:
107: return logger.isTraceEnabled(); // ALL
108: }
109: }
110:
111: /**
112: * Logs a record at the specified level.
113: */
114: public void log(final Level level, final String message,
115: final Throwable thrown) {
116: final int n = level.intValue();
117: switch (n / 100) {
118: default: {
119: // MAX_VALUE is a special value for Level.OFF. Otherwise and
120: // if positive, log to fatal since we are greater than SEVERE.
121: if (n != Integer.MAX_VALUE || n >= 0) {
122: logger.fatal(message, thrown);
123: }
124: break;
125: }
126: case 10:
127: logger.error(message, thrown);
128: break; // SEVERE
129: case 9:
130: logger.warn(message, thrown);
131: break; // WARNING
132: case 8: // INFO
133: case 7:
134: logger.info(message, thrown);
135: break; // CONFIG
136: case 6: // (not allocated)
137: case 5:
138: logger.debug(message, thrown);
139: break; // FINE
140: case 4: // FINER
141: case 3: // FINEST
142: case 2: // (not allocated)
143: case 1: // (not allocated)
144: case 0:
145: logger.trace(message, thrown);
146: break; // ALL
147: }
148: }
149:
150: public void severe(String message) {
151: logger.error(message);
152: }
153:
154: public void warning(String message) {
155: logger.warn(message);
156: }
157:
158: public void info(String message) {
159: logger.info(message);
160: }
161:
162: public void config(String message) {
163: logger.info(message);
164: }
165:
166: public void fine(String message) {
167: logger.debug(message);
168: }
169:
170: public void finer(String message) {
171: logger.debug(message);
172: }
173:
174: public void finest(String message) {
175: logger.trace(message);
176: }
177: }
|