KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > log4j > Log4jLogger


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/wrappers/log4j/Log4jLogger.java,v 1.13 2004/07/28 09:32:05 ib Exp $
3  * $Revision: 1.13 $
4  * $Date: 2004/07/28 09:32:05 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package log4j;
25
26 import org.apache.log4j.Category;
27 import org.apache.log4j.Level;
28 import org.apache.slide.util.logger.Logger;
29
30 /**
31  * Log4j logger implementation.
32  *
33  */

34 public class Log4jLogger implements Logger {
35     
36     
37     // ----------------------------------------------------- Instance Variables
38

39     
40     
41     
42     // ----------------------------------------------------- Static Initializer
43

44     
45     static {
46 // BasicConfigurator.configure();
47
}
48     
49     
50     // ------------------------------------------------------------- Properties
51

52     
53     /**
54      * Logger level setter.
55      *
56      * @param loggerLevel New logger level
57      */

58     public void setLoggerLevel(String JavaDoc channel, int loggerLevel) {
59         Level level = toLevel(loggerLevel);
60         Category cat = getCategory(channel);
61         cat.setLevel(level);
62     }
63     
64     /**
65      * Logger level setter.
66      *
67      * @param loggerLevel New logger level
68      */

69     public void setLoggerLevel(int loggerLevel) {
70         setLoggerLevel(DEFAULT_CHANNEL, loggerLevel);
71     }
72     
73     
74     /**
75      * Logger level getter.
76      *
77      * @return int logger level
78      */

79     public int getLoggerLevel(String JavaDoc channel) {
80         Category cat = getCategory(channel);
81         return fromLevel(cat.getLevel());
82     }
83     
84     
85     /**
86      * Logger level getter.
87      *
88      * @return int logger level
89      */

90     public int getLoggerLevel() {
91         return getLoggerLevel(DEFAULT_CHANNEL);
92     }
93     
94     
95     
96     
97     // --------------------------------------------------------- Logger Methods
98

99     
100     /**
101      * Log an object and an associated throwable thru the specified channel and with the specified level.
102      *
103      * @param data object to log
104      * @param throwable throwable to be logged
105      * @param channel channel name used for logging
106      * @param level level used for logging
107      */

108     public void log(Object JavaDoc data, Throwable JavaDoc throwable, String JavaDoc channel, int level) {
109         Category cat = getCategory(channel);
110         cat.log(toLevel(level), data, throwable);
111     }
112
113     
114     /**
115      * Log an object thru the specified channel and with the specified level.
116      *
117      * @param data The object to log.
118      * @param channel The channel name used for logging.
119      * @param level The level used for logging.
120      */

121     public void log(Object JavaDoc data, String JavaDoc channel, int level) {
122         Category cat = getCategory(channel);
123         if (data instanceof Throwable JavaDoc)
124             cat.log(toLevel(level), data, (Throwable JavaDoc)data);
125         else
126             cat.log(toLevel(level), data);
127     }
128     
129     
130     /**
131      * Log an object with the specified level.
132      *
133      * @param data The object to log.
134      * @param level The level used for logging.
135      */

136     public void log(Object JavaDoc data, int level) {
137         this.log(data, DEFAULT_CHANNEL, level);
138     }
139     
140     
141     /**
142      * Log an object.
143      *
144      * @param data The object to log.
145      */

146     public void log(Object JavaDoc data) {
147         this.log(data, DEFAULT_CHANNEL, Logger.DEBUG);
148     }
149     
150         
151     
152     /**
153      * Check if the channel with the specified level is enabled for logging.
154      * This implementation ignores the channel specification
155      *
156      * @param channel The channel specification
157      * @param level The level specification
158      */

159
160     public boolean isEnabled(String JavaDoc channel, int level) {
161         Category cat = getCategory(channel);
162         return cat.isEnabledFor(toLevel(level));
163     }
164             
165         
166     
167     /**
168      * Check if the default channel with the specified level is enabled for logging.
169      *
170      * @param level The level specification
171      */

172
173     public boolean isEnabled(int level) {
174         return isEnabled(DEFAULT_CHANNEL, level);
175     }
176             
177     
178     
179     
180     // ------------------------------------------------------ Private/Protected Methods
181

182     private Category getCategory(String JavaDoc channel) {
183         Category cat = Category.exists(channel);
184         if (cat == null) {
185             cat = Category.getInstance(channel);
186         }
187         return cat;
188     }
189     
190     
191     /**
192      * Convert Slide level to log4j level.
193      */

194     protected Level toLevel(int level) {
195         Level result = null;
196         
197         switch (level) {
198         case EMERGENCY:
199             result = Level.FATAL;
200             break;
201         case CRITICAL:
202             result = Level.FATAL;
203             break;
204         case ERROR:
205             result = Level.ERROR;
206             break;
207         case WARNING:
208             result = Level.WARN;
209             break;
210         case INFO:
211             result = Level.INFO;
212             break;
213         case DEBUG:
214             result = Level.DEBUG;
215             break;
216         }
217         
218         if (result == null)
219             result = Level.toLevel(level);
220         return result;
221     }
222     
223     
224     /**
225      * Convert Slide level to log4j level.
226      */

227     protected int fromLevel(Level level) {
228         int result = INFO;
229         
230         if (level.equals(Level.FATAL)){
231             result = EMERGENCY;
232         }
233         else if (level.equals(Level.ERROR)){
234             result = ERROR;
235         }
236         else if (level.equals(Level.WARN)){
237             result = WARNING;
238         }
239         else if (level.equals(Level.INFO)){
240             result = INFO;
241         }
242         else if (level.equals(Level.DEBUG)){
243             result = DEBUG;
244         }
245         else {
246             result = INFO;
247         }
248         
249         return result;
250     }
251
252 }
253
Popular Tags