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: */package org.apache.geronimo.system.logging.log4j;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.log4j.Level;
020: import org.apache.log4j.Logger;
021:
022: /**
023: * This log wrapper caches the trace, debug and info enabled flags. The flags are updated
024: * by a single timer task for all logs.
025: *
026: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
027: */
028: public final class CachingLog4jLog implements Log {
029: private final String FQCN = getClass().getName();
030: private Logger logger;
031: private boolean traceEnabled;
032: private boolean debugEnabled;
033: private boolean infoEnabled;
034:
035: public CachingLog4jLog(String name) {
036: logger = Logger.getLogger(name);
037: updateLevelInfo();
038: }
039:
040: public CachingLog4jLog(Logger logger) {
041: this .logger = logger;
042: updateLevelInfo();
043: }
044:
045: public boolean isTraceEnabled() {
046: return traceEnabled;
047: }
048:
049: public void trace(Object message) {
050: if (traceEnabled) {
051: logger.log(FQCN, XLevel.TRACE, message, null);
052: }
053: }
054:
055: public void trace(Object message, Throwable throwable) {
056: if (traceEnabled) {
057: logger.log(FQCN, XLevel.TRACE, message, throwable);
058: }
059: }
060:
061: public boolean isDebugEnabled() {
062: return debugEnabled;
063: }
064:
065: public void debug(Object message) {
066: if (debugEnabled) {
067: logger.log(FQCN, Level.DEBUG, message, null);
068: }
069: }
070:
071: public void debug(Object message, Throwable throwable) {
072: if (debugEnabled) {
073: logger.log(FQCN, Level.DEBUG, message, throwable);
074: }
075: }
076:
077: public boolean isInfoEnabled() {
078: return infoEnabled;
079: }
080:
081: public void info(Object message) {
082: if (infoEnabled) {
083: logger.log(FQCN, Level.INFO, message, null);
084: }
085: }
086:
087: public void info(Object message, Throwable throwable) {
088: if (infoEnabled) {
089: logger.log(FQCN, Level.INFO, message, throwable);
090: }
091: }
092:
093: public boolean isWarnEnabled() {
094: return logger.isEnabledFor(Level.WARN);
095: }
096:
097: public void warn(Object message) {
098: logger.log(FQCN, Level.WARN, message, null);
099: }
100:
101: public void warn(Object message, Throwable throwable) {
102: logger.log(FQCN, Level.WARN, message, throwable);
103: }
104:
105: public boolean isErrorEnabled() {
106: return logger.isEnabledFor(Level.ERROR);
107: }
108:
109: public void error(Object message) {
110: logger.log(FQCN, Level.ERROR, message, null);
111: }
112:
113: public void error(Object message, Throwable throwable) {
114: logger.log(FQCN, Level.ERROR, message, throwable);
115: }
116:
117: public boolean isFatalEnabled() {
118: return logger.isEnabledFor(Level.FATAL);
119: }
120:
121: public void fatal(Object message) {
122: logger.log(FQCN, Level.FATAL, message, null);
123: }
124:
125: public void fatal(Object message, Throwable throwable) {
126: logger.log(FQCN, Level.FATAL, message, throwable);
127: }
128:
129: public void updateLevelInfo() {
130: // This method is proposely not synchronized.
131: // The setting of a boolean is atomic so we don't have to worry about inconsistent state.
132: // Normally we would have to worry about an out of date cache running threads (SMP boxes),
133: // but this cache is not time critical (so don't worry about it).
134: traceEnabled = logger.isEnabledFor(XLevel.TRACE);
135: debugEnabled = logger.isDebugEnabled();
136: infoEnabled = logger.isInfoEnabled();
137: }
138: }
|