001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 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:
020: /**
021: * An adapter that redirect all Java logging events to the Apache's
022: * <A HREF="http://logging.apache.org/log4j">Log4J</A> framework.
023: *
024: * @since 2.4
025: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/logging/Log4JLogger.java $
026: * @version $Id: Log4JLogger.java 27891 2007-11-14 14:10:48Z desruisseaux $
027: * @author Martin Desruisseaux
028: * @author Saul Farber
029: *
030: * @see Log4JLoggerFactory
031: * @see Logging
032: */
033: final class Log4JLogger extends LoggerAdapter {
034: /**
035: * The Log4J logger to use.
036: */
037: final org.apache.log4j.Logger logger;
038:
039: /**
040: * Creates a new logger.
041: *
042: * @param name The logger name.
043: * @param logger The result of {@code Logger.getLogger(name)}.
044: */
045: public Log4JLogger(final String name,
046: final org.apache.log4j.Logger logger) {
047: super (name);
048: this .logger = logger;
049: }
050:
051: /**
052: * Returns the Log4J level for the given Java level.
053: */
054: private static org.apache.log4j.Level toLog4JLevel(final Level level) {
055: final int n = level.intValue();
056: switch (n / 100) {
057: default: {
058: // MAX_VALUE is a special value for Level.OFF. Otherwise and
059: // if positive, log to fatal since we are greater than SEVERE.
060: switch (n) {
061: default:
062: if (n >= 0)
063: return org.apache.log4j.Level.FATAL; // fallthrough ALL otherwise.
064: case Integer.MIN_VALUE:
065: return org.apache.log4j.Level.ALL;
066: case Integer.MAX_VALUE:
067: return org.apache.log4j.Level.OFF;
068: }
069: }
070: case 10:
071: return org.apache.log4j.Level.ERROR; // SEVERE
072: case 9:
073: return org.apache.log4j.Level.WARN; // WARNING
074: case 8: // INFO
075: case 7:
076: return org.apache.log4j.Level.INFO; // CONFIG
077: case 6: // (not allocated)
078: case 5:
079: return org.apache.log4j.Level.DEBUG; // FINE
080: case 4:
081: return org.apache.log4j.Level.TRACE; // FINER
082: case 3: // FINEST
083: case 2: // (not allocated)
084: case 1: // (not allocated)
085: case 0:
086: return org.apache.log4j.Level.ALL; // ALL
087: }
088: }
089:
090: /**
091: * Returns the Java level for the given Log4J level.
092: */
093: private static Level toJavaLevel(final org.apache.log4j.Level level) {
094: final int n = level.toInt();
095: if (n == org.apache.log4j.Level.OFF_INT)
096: return Level.OFF;
097: if (n >= org.apache.log4j.Level.ERROR_INT)
098: return Level.SEVERE;
099: if (n >= org.apache.log4j.Level.WARN_INT)
100: return Level.WARNING;
101: if (n >= org.apache.log4j.Level.INFO_INT)
102: return Level.CONFIG;
103: if (n >= org.apache.log4j.Level.DEBUG_INT)
104: return Level.FINE;
105: if (n >= org.apache.log4j.Level.TRACE_INT)
106: return Level.FINER;
107: return Level.ALL;
108: }
109:
110: /**
111: * Set the level for this logger.
112: */
113: public void setLevel(final Level level) {
114: logger.setLevel(toLog4JLevel(level));
115: }
116:
117: /**
118: * Returns the level for this logger.
119: */
120: public Level getLevel() {
121: return toJavaLevel(logger.getEffectiveLevel());
122: }
123:
124: /**
125: * Returns {@code true} if the specified level is loggable.
126: */
127: public boolean isLoggable(final Level level) {
128: return logger.isEnabledFor(toLog4JLevel(level));
129: }
130:
131: /**
132: * Logs a record at the specified level.
133: */
134: public void log(final Level level, final String message) {
135: logger.log(toLog4JLevel(level), message);
136: }
137:
138: /**
139: * Logs a record at the specified level.
140: */
141: public void log(final Level level, final String message,
142: final Throwable thrown) {
143: logger.log(toLog4JLevel(level), message, thrown);
144: }
145:
146: public void severe(String message) {
147: logger.error(message);
148: }
149:
150: public void warning(String message) {
151: logger.warn(message);
152: }
153:
154: public void info(String message) {
155: logger.info(message);
156: }
157:
158: public void config(String message) {
159: logger.info(message);
160: }
161:
162: public void fine(String message) {
163: logger.debug(message);
164: }
165:
166: public void finer(String message) {
167: logger.debug(message);
168: }
169:
170: public void finest(String message) {
171: logger.trace(message);
172: }
173: }
|