KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > ojb > components > LoggerImpl


1 /*
2  * Copyright 1999-2002,2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.ojb.components;
17
18 import org.apache.avalon.framework.activity.Initializable;
19 import org.apache.avalon.framework.component.Component;
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import org.apache.ojb.broker.util.configuration.Configuration;
24 import org.apache.ojb.broker.util.configuration.ConfigurationException;
25 import org.apache.ojb.broker.util.logging.Logger;
26 import org.apache.ojb.broker.util.logging.LoggingConfiguration;
27
28 /**
29  * OJB logger implementation delegating to the Avalon logger.
30  *
31  * <p>This class has two faces to it:
32  * <dl>
33  * <dt>Avalon Component</dt>
34  * <dd>Instance of the class created and managed by Avalon container.
35  * When instance is initialized, it obtains logger instance to be used
36  * by OJB.</dd>
37  * <dt>OJB Managed Class</dt>
38  * <dd>Instances of the class are created and managed by OJB, as defined
39  * in the OJB <code>OJB.properties</code> file. Each OJB managed instance
40  * of the class will have access to the logger object initialized
41  * by Avalon managed instance of the class.</dd>
42  * </dl>
43  *
44  * It is important that Avalon component is initialized before any access
45  * to OJB API is made.</p>
46  *
47  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
48  * @version $Id: LoggerImpl.java 325970 2005-10-17 19:48:08Z vgritsenko $
49  */

50 public class LoggerImpl extends AbstractLogEnabled
51                         implements Component, ThreadSafe, Initializable,
52                                    Logger {
53
54     /**
55      * Root logger for all OJB loggers
56      */

57     private static org.apache.avalon.framework.logger.Logger LOGGER;
58
59     private final String JavaDoc name;
60     private transient int level;
61     private transient org.apache.avalon.framework.logger.Logger logger;
62
63     /**
64      * Constructor used by Container
65      */

66     public LoggerImpl() {
67         this.name = null;
68     }
69
70     /**
71      * Constructor used by OJB 1.0 to create a logger instance
72      */

73     public LoggerImpl(String JavaDoc name) {
74         this.name = name.startsWith("org.apache.ojb.")? name.substring(15): name;
75     }
76
77     /**
78      * Constructor used by OJB 1.1 to create a logger instance
79      */

80     public LoggerImpl(String JavaDoc name, LoggingConfiguration config) {
81         this(name);
82     }
83
84     /**
85      * Set root logger instance which will be used by OJB
86      */

87     public void initialize() {
88         LOGGER = getLogger();
89     }
90
91     protected int getLevel() {
92         if (logger == null) {
93             this.logger = LOGGER.getChildLogger(this.name);
94             if (this.logger.isDebugEnabled()) this.level = DEBUG;
95             else if (this.logger.isInfoEnabled()) this.level = INFO;
96             else if (this.logger.isWarnEnabled()) this.level = WARN;
97             else if (this.logger.isErrorEnabled()) this.level = ERROR;
98             else this.level = FATAL;
99         }
100         return level;
101     }
102
103     public String JavaDoc getName() {
104         return name;
105     }
106
107     public void debug(Object JavaDoc message) {
108         if (DEBUG >= getLevel()) {
109             logger.debug(toString(message));
110         }
111     }
112
113     public void debug(Object JavaDoc message, Throwable JavaDoc t) {
114         if (DEBUG >= getLevel()) {
115             logger.debug(toString(message), t);
116         }
117     }
118
119     public void safeDebug(String JavaDoc message, Object JavaDoc obj) {
120         if (DEBUG >= getLevel()) {
121             logger.debug(message + " : " + toString(obj));
122         }
123     }
124
125     public void safeDebug(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t) {
126         if (DEBUG >= getLevel()) {
127             logger.debug(message + " : " + toString(obj), t);
128         }
129     }
130
131     public void info(Object JavaDoc message) {
132         if (INFO >= getLevel()) {
133             logger.info(toString(message));
134         }
135     }
136
137     public void info(Object JavaDoc message, Throwable JavaDoc t) {
138         if (INFO >= getLevel()) {
139             logger.info(toString(message), t);
140         }
141     }
142
143     public void safeInfo(String JavaDoc message, Object JavaDoc obj) {
144         if (INFO >= getLevel()) {
145             logger.info(message + " : " + toString(obj));
146         }
147     }
148
149     public void safeInfo(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t) {
150         if (INFO >= getLevel()) {
151             logger.info(message + " : " + toString(obj), t);
152         }
153     }
154
155     public void warn(Object JavaDoc message) {
156         if (WARN >= getLevel()) {
157             logger.warn(toString(message));
158         }
159     }
160
161     public void warn(Object JavaDoc message, Throwable JavaDoc t) {
162         if (WARN >= getLevel()) {
163             logger.warn(toString(message), t);
164         }
165     }
166
167     public void safeWarn(String JavaDoc message, Object JavaDoc obj) {
168         if (WARN >= getLevel()) {
169             logger.warn(message + " : " + toString(obj));
170         }
171     }
172
173     public void safeWarn(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t) {
174         if (WARN >= getLevel()) {
175             logger.warn(message + " : " + toString(obj), t);
176         }
177     }
178
179     public void error(Object JavaDoc message) {
180         if (ERROR >= getLevel()) {
181             logger.error(toString(message));
182         }
183     }
184
185     public void error(Object JavaDoc message, Throwable JavaDoc t) {
186         if (ERROR >= getLevel()) {
187             logger.error(toString(message), t);
188         }
189     }
190
191     public void safeError(String JavaDoc message, Object JavaDoc obj) {
192         if (ERROR >= getLevel()) {
193             logger.error(message + " : " + toString(obj));
194         }
195     }
196
197     public void safeError(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t) {
198         if (ERROR >= getLevel()) {
199             logger.error(message + " : " + toString(obj), t);
200         }
201     }
202
203     public void fatal(Object JavaDoc message) {
204         if (FATAL >= getLevel()) {
205             logger.fatalError(toString(message));
206         }
207     }
208
209     public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
210         if (FATAL >= getLevel()) {
211             logger.fatalError(toString(message), t);
212         }
213     }
214
215     public void safeFatal(String JavaDoc message, Object JavaDoc obj) {
216         if (FATAL >= getLevel()) {
217             logger.fatalError(message + " : " + toString(obj));
218         }
219     }
220
221     public void safeFatal(String JavaDoc message, Object JavaDoc obj, Throwable JavaDoc t) {
222         if (FATAL >= getLevel()) {
223             logger.fatalError(message + " : " + toString(obj), t);
224         }
225     }
226
227     public boolean isDebugEnabled() {
228         return isEnabledFor(DEBUG);
229     }
230
231     public boolean isEnabledFor(int priority) {
232         return priority >= getLevel();
233     }
234
235     /*
236      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
237      */

238     public void configure(Configuration config) throws ConfigurationException {
239     }
240
241     private String JavaDoc toString(Object JavaDoc obj) {
242         if (obj != null) {
243             try {
244                 return obj.toString();
245             } catch (Throwable JavaDoc throwable) {
246                 return "BAD toString() impl for " + obj.getClass().getName();
247             }
248         }
249
250         return "null";
251     }
252 }
253
Popular Tags