View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.system.logging.log4j;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.log4j.Level;
22  import org.apache.log4j.Logger;
23  
24  /**
25   * This log wrapper caches the trace, debug and info enabled flags.  The flags are updated
26   * by a single timer task for all logs.
27   *
28   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
29   */
30  public final class CachingLog4jLog implements Log {
31      private final String FQCN = getClass().getName();
32      private Logger logger;
33      private boolean traceEnabled;
34      private boolean debugEnabled;
35      private boolean infoEnabled;
36  
37      public CachingLog4jLog(String name) {
38          logger = Logger.getLogger(name);
39          updateLevelInfo();
40      }
41  
42      public CachingLog4jLog(Logger logger) {
43          this.logger = logger;
44          updateLevelInfo();
45      }
46  
47      public boolean isTraceEnabled() {
48          return traceEnabled;
49      }
50  
51      public void trace(Object message) {
52          if (traceEnabled) {
53              logger.log(FQCN, XLevel.TRACE, message, null);
54          }
55      }
56  
57      public void trace(Object message, Throwable throwable) {
58          if (traceEnabled) {
59              logger.log(FQCN, XLevel.TRACE, message, throwable);
60          }
61      }
62  
63      public boolean isDebugEnabled() {
64          return debugEnabled;
65      }
66  
67      public void debug(Object message) {
68          if (debugEnabled) {
69              logger.log(FQCN, Level.DEBUG, message, null);
70          }
71      }
72  
73      public void debug(Object message, Throwable throwable) {
74          if (debugEnabled) {
75              logger.log(FQCN, Level.DEBUG, message, throwable);
76          }
77      }
78  
79      public boolean isInfoEnabled() {
80          return infoEnabled;
81      }
82  
83      public void info(Object message) {
84          if (infoEnabled) {
85              logger.log(FQCN, Level.INFO, message, null);
86          }
87      }
88  
89      public void info(Object message, Throwable throwable) {
90          if (infoEnabled) {
91              logger.log(FQCN, Level.INFO, message, throwable);
92          }
93      }
94  
95      public boolean isWarnEnabled() {
96          return logger.isEnabledFor(Level.WARN);
97      }
98  
99      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 }