KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > logging > log4j > CachingLog4jLog


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
29  */

30 public final class CachingLog4jLog implements Log {
31     private final String JavaDoc FQCN = getClass().getName();
32     private Logger logger;
33     private boolean traceEnabled;
34     private boolean debugEnabled;
35     private boolean infoEnabled;
36
37     public CachingLog4jLog(String JavaDoc 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 JavaDoc message) {
52         if (traceEnabled) {
53             logger.log(FQCN, XLevel.TRACE, message, null);
54         }
55     }
56
57     public void trace(Object JavaDoc message, Throwable JavaDoc 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 JavaDoc message) {
68         if (debugEnabled) {
69             logger.log(FQCN, Level.DEBUG, message, null);
70         }
71     }
72
73     public void debug(Object JavaDoc message, Throwable JavaDoc 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 JavaDoc message) {
84         if (infoEnabled) {
85             logger.log(FQCN, Level.INFO, message, null);
86         }
87     }
88
89     public void info(Object JavaDoc message, Throwable JavaDoc 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 JavaDoc message) {
100         logger.log(FQCN, Level.WARN, message, null);
101     }
102
103     public void warn(Object JavaDoc message, Throwable JavaDoc 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 JavaDoc message) {
112         logger.log(FQCN, Level.ERROR, message, null);
113     }
114
115     public void error(Object JavaDoc message, Throwable JavaDoc 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 JavaDoc message) {
124         logger.log(FQCN, Level.FATAL, message, null);
125     }
126
127     public void fatal(Object JavaDoc message, Throwable JavaDoc 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 }
141
Popular Tags