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