001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import org.apache.log4j.Level;
011: import org.apache.log4j.Priority;
012: import org.codehaus.dna.Logger;
013:
014: /**
015: * Logging facade implmentation for Apache Log4J project.
016: * The following lists the mapping between DNA log levels
017: * and Log4J log levels.
018: *
019: * <ul>
020: * <li>trace ==> debug</li>
021: * <li>debug ==> debug</li>
022: * <li>info ==> info</li>
023: * <li>warn ==> warn</li>
024: * <li>error ==> error</li>
025: * </ul>
026: *
027: * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
028: */
029: public class Log4JLogger implements Logger {
030: /**
031: * The fully qualified name of the current class so
032: * Log4J will not include it in traces.
033: */
034: private static final String FQCN = Log4JLogger.class.getName();
035:
036: /**
037: * The log4j logger instance.
038: */
039: private final org.apache.log4j.Logger m_logger;
040:
041: /**
042: * Create an instance of Log4J facade.
043: *
044: * @param logger the log4j logger
045: */
046: public Log4JLogger(final org.apache.log4j.Logger logger) {
047: if (null == logger) {
048: throw new NullPointerException("logger");
049: }
050: m_logger = logger;
051: }
052:
053: /**
054: * Log a trace message.
055: *
056: * @param message the message
057: */
058: public void trace(final String message) {
059: m_logger.log(FQCN, Level.DEBUG, message, null);
060: }
061:
062: /**
063: * Log a trace message with an associated throwable.
064: *
065: * @param message the message
066: * @param throwable the throwable
067: */
068: public void trace(final String message, final Throwable throwable) {
069: m_logger.log(FQCN, Level.DEBUG, message, throwable);
070: }
071:
072: /**
073: * Return true if a trace message will be logged.
074: *
075: * @return true if message will be logged
076: */
077: public boolean isTraceEnabled() {
078: return m_logger.isDebugEnabled();
079: }
080:
081: /**
082: * Log a debug message.
083: *
084: * @param message the message
085: */
086: public void debug(final String message) {
087: m_logger.log(FQCN, Level.DEBUG, message, null);
088: }
089:
090: /**
091: * Log a debug message with an associated throwable.
092: *
093: * @param message the message
094: * @param throwable the throwable
095: */
096: public void debug(final String message, final Throwable throwable) {
097: m_logger.log(FQCN, Level.DEBUG, message, throwable);
098: }
099:
100: /**
101: * Return true if a debug message will be logged.
102: *
103: * @return true if message will be logged
104: */
105: public boolean isDebugEnabled() {
106: return m_logger.isDebugEnabled();
107: }
108:
109: /**
110: * Log a info message.
111: *
112: * @param message the message
113: */
114: public void info(final String message) {
115: m_logger.log(FQCN, Level.INFO, message, null);
116: }
117:
118: /**
119: * Log a info message with an associated throwable.
120: *
121: * @param message the message
122: * @param throwable the throwable
123: */
124: public void info(final String message, final Throwable throwable) {
125: m_logger.log(FQCN, Level.INFO, message, throwable);
126: }
127:
128: /**
129: * Return true if an info message will be logged.
130: *
131: * @return true if message will be logged
132: */
133: public boolean isInfoEnabled() {
134: return m_logger.isInfoEnabled();
135: }
136:
137: /**
138: * Log a warn message.
139: *
140: * @param message the message
141: */
142: public void warn(final String message) {
143: m_logger.log(FQCN, Level.WARN, message, null);
144: }
145:
146: /**
147: * Log a warn message with an associated throwable.
148: *
149: * @param message the message
150: * @param throwable the throwable
151: */
152: public void warn(final String message, final Throwable throwable) {
153: m_logger.log(FQCN, Level.WARN, message, throwable);
154: }
155:
156: /**
157: * Return true if a warn message will be logged.
158: *
159: * @return true if message will be logged
160: */
161: public boolean isWarnEnabled() {
162: return m_logger.isEnabledFor(Priority.WARN);
163: }
164:
165: /**
166: * Log a error message.
167: *
168: * @param message the message
169: */
170: public void error(final String message) {
171: m_logger.log(FQCN, Level.ERROR, message, null);
172: }
173:
174: /**
175: * Log a error message with an associated throwable.
176: *
177: * @param message the message
178: * @param throwable the throwable
179: */
180: public void error(final String message, final Throwable throwable) {
181: m_logger.log(FQCN, Level.ERROR, message, throwable);
182: }
183:
184: /**
185: * Return true if a error message will be logged.
186: *
187: * @return true if message will be logged
188: */
189: public boolean isErrorEnabled() {
190: return m_logger.isEnabledFor(Priority.ERROR);
191: }
192:
193: /**
194: * Get the child logger with specified name.
195: *
196: * @param name the name of child logger
197: * @return the child logger
198: */
199: public Logger getChildLogger(final String name) {
200:
201: return new Log4JLogger(org.apache.log4j.Logger
202: .getLogger(m_logger.getName() + "." + name));
203: }
204: }
|