001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.util.logging.internal;
020:
021: import java.io.StringWriter;
022: import java.io.PrintWriter;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.log4j.Level;
026: import org.apache.log4j.Logger;
027: import org.apache.log4j.Category;
028:
029: /**
030: * <p>
031: * Logging abstraction used to pipe log messages to Log4J. This class is used for
032: * NetUI backwards compatability so that previous {@link org.apache.beehive.netui.util.logging.Logger}
033: * clients continue to log through the usual Log4J channels.
034: * </p>
035: *
036: * @deprecated
037: */
038: public final class Log4JLogger implements Log {
039:
040: private static final String STRUTS_APPENDER = "commons-logging";
041:
042: static {
043: // Need to get rid of the appender that Struts adds so
044: // that we don't spam the console with all messages
045: Category root = Category.getRoot();
046:
047: if (root.getAppender(STRUTS_APPENDER) != null)
048: root.removeAppender(STRUTS_APPENDER);
049: }
050:
051: private Logger _logInstance;
052:
053: public static Log getInstance(Class clazz) {
054: return new Log4JLogger(clazz);
055: }
056:
057: private Log4JLogger(Class clazz) {
058: this (clazz.getName());
059: }
060:
061: private Log4JLogger(String className) {
062: _logInstance = Logger.getLogger(className);
063: }
064:
065: public boolean isDebugEnabled() {
066: return _logInstance.isEnabledFor(Level.DEBUG);
067: }
068:
069: public boolean isErrorEnabled() {
070: return _logInstance.isEnabledFor(Level.ERROR);
071: }
072:
073: public boolean isFatalEnabled() {
074: return _logInstance.isEnabledFor(Level.FATAL);
075: }
076:
077: public boolean isInfoEnabled() {
078: return _logInstance.isEnabledFor(Level.INFO);
079: }
080:
081: public boolean isTraceEnabled() {
082: return _logInstance.isEnabledFor(Level.DEBUG);
083: }
084:
085: public boolean isWarnEnabled() {
086: return _logInstance.isEnabledFor(Level.WARN);
087: }
088:
089: public void debug(Object message) {
090: if (_logInstance.isEnabledFor(Level.DEBUG))
091: _logInstance.debug(message);
092: }
093:
094: public void debug(Object message, Throwable t) {
095: if (_logInstance.isEnabledFor(Level.DEBUG))
096: _logInstance.debug(format(message, t));
097: }
098:
099: public void trace(Object message) {
100: if (_logInstance.isEnabledFor(Level.DEBUG))
101: _logInstance.debug(message);
102: }
103:
104: public void trace(Object message, Throwable t) {
105: if (_logInstance.isEnabledFor(Level.DEBUG))
106: _logInstance.debug(format(message, t));
107: }
108:
109: public void info(Object message) {
110: if (_logInstance.isEnabledFor(Level.INFO))
111: _logInstance.info(message);
112: }
113:
114: public void info(Object message, Throwable t) {
115: if (_logInstance.isEnabledFor(Level.INFO))
116: _logInstance.info(format(message, t));
117: }
118:
119: public void warn(Object message) {
120: if (_logInstance.isEnabledFor(Level.WARN))
121: _logInstance.warn(message);
122: }
123:
124: public void warn(Object message, Throwable t) {
125: if (_logInstance.isEnabledFor(Level.WARN))
126: _logInstance.warn(format(message, t));
127: }
128:
129: public void error(Object message) {
130: if (_logInstance.isEnabledFor(Level.ERROR))
131: _logInstance.error(message);
132: }
133:
134: public void error(Object message, Throwable t) {
135: if (_logInstance.isEnabledFor(Level.ERROR))
136: _logInstance.error(format(message, t));
137: }
138:
139: public void fatal(Object message) {
140: if (_logInstance.isEnabledFor(Level.FATAL))
141: _logInstance.fatal(message);
142: }
143:
144: public void fatal(Object message, Throwable t) {
145: if (_logInstance.isEnabledFor(Level.FATAL))
146: _logInstance.fatal(format(message, t));
147: }
148:
149: private String format(Object m, Throwable t) {
150: if (t == null)
151: return m.toString();
152:
153: StringWriter sw = new StringWriter();
154: t.printStackTrace(new PrintWriter(sw));
155:
156: return m + "\n\n" + "Throwable: " + t.toString()
157: + "\nStack Trace:\n" + sw.toString();
158: }
159: }
|