001: package org.apache.ojb.broker.util.logging;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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: import java.net.URL;
019: import java.util.Enumeration;
020:
021: import org.apache.log4j.LogManager;
022: import org.apache.log4j.Level;
023: import org.apache.log4j.PropertyConfigurator;
024: import org.apache.ojb.broker.util.ClassHelper;
025: import org.apache.ojb.broker.util.configuration.Configuration;
026: import org.apache.ojb.broker.util.configuration.ConfigurationException;
027:
028: /**
029: * This is a Logger implementation based on Log4j.
030: * It can be enabled by putting
031: * LoggerClass=org.apache.ojb.broker.util.logging.Log4jLoggerImpl
032: * in the OJB .properties file. <br>
033: * If you want log4j to initialize from a property file you can add
034: * LoggerConfigFile=log4j.properties to the org.apache.ojb.properties file.
035: * the logger only initializes log4j if the application hasn't done it yet
036: *
037: * You can find sample log4j.properties file in the log4j web site
038: * http://jakarta.apache.org/log4j
039: * in the javadoc look for org.apache.log4j.examples
040: *
041: * @author Bertrand
042: * @author Thomas Mahler
043: * @version $Id: Log4jLoggerImpl.java,v 1.16.2.4 2005/12/21 22:28:16 tomdz Exp $
044: */
045: public class Log4jLoggerImpl implements Logger {
046: static private final String FQCN = Log4jLoggerImpl.class.getName();
047: /** flag about log4j configuration state */
048: private static boolean log4jConfigured = false;
049:
050: private transient org.apache.log4j.Logger logger;
051: private String name;
052:
053: /** Helper method to check if log4j is already configured */
054: private static synchronized boolean isLog4JConfigured() {
055: if (!log4jConfigured) {
056: Enumeration en = org.apache.log4j.Logger.getRootLogger()
057: .getAllAppenders();
058:
059: if (!(en instanceof org.apache.log4j.helpers.NullEnumeration)) {
060: log4jConfigured = true;
061: } else {
062: Enumeration cats = LogManager.getCurrentLoggers();
063: while (cats.hasMoreElements()) {
064: org.apache.log4j.Logger c = (org.apache.log4j.Logger) cats
065: .nextElement();
066: if (!(c.getAllAppenders() instanceof org.apache.log4j.helpers.NullEnumeration)) {
067: log4jConfigured = true;
068: }
069: }
070: }
071: if (log4jConfigured) {
072: String msg = "Log4J is already configured, will not search for log4j properties file";
073: LoggerFactory.getBootLogger().info(msg);
074: } else {
075: LoggerFactory.getBootLogger().info(
076: "Log4J is not configured");
077: }
078: }
079: return log4jConfigured;
080: }
081:
082: /**
083: * Initialization of log4j <br>
084: * <b>NOTE</b> - if log4j property file is called log4j.properties then
085: * log4j will be configured already.
086: */
087: private static synchronized void initializeLog4JSubSystem(
088: String configFile) {
089: LoggerFactory.getBootLogger().info(
090: "Initializing Log4J using file: '" + configFile + "'");
091: if (configFile == null || "".equals(configFile.trim())) {
092: // no configuration available
093: LoggerFactory.getBootLogger().warn(
094: "No log4j configuration file specified");
095: } else {
096: // try resource look in classpath
097: URL url = ClassHelper.getResource(configFile);
098: LoggerFactory.getBootLogger().info(
099: "Initializing Log4J : resource from config file:"
100: + url);
101: if (url != null) {
102: PropertyConfigurator.configure(url);
103: }
104: // if file is not in classpath try ordinary filesystem lookup
105: else {
106: PropertyConfigurator.configure(configFile);
107: }
108: }
109: log4jConfigured = true;
110: }
111:
112: public Log4jLoggerImpl(String name) {
113: this .name = name;
114: }
115:
116: /**
117: * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
118: * This method must be performed by LogFactory after creating a logger instance.
119: */
120: public void configure(Configuration config)
121: throws ConfigurationException {
122: if (!isLog4JConfigured()) {
123: LoggingConfiguration lc = (LoggingConfiguration) config;
124: initializeLog4JSubSystem(lc.getLoggerConfigFile());
125: }
126: }
127:
128: /**
129: * Gets the logger.
130: *
131: * @return Returns a Category
132: */
133: private org.apache.log4j.Logger getLogger() {
134: /*
135: Logger interface extends Serializable, thus Log field is
136: declared 'transient' and we have to null-check
137: */
138: if (logger == null) {
139: logger = org.apache.log4j.Logger.getLogger(name);
140: }
141: return logger;
142: }
143:
144: public String getName() {
145: return name;
146: }
147:
148: private Level getLevel() {
149: return getLogger().getEffectiveLevel();
150: }
151:
152: /**
153: * generate a message for loglevel DEBUG
154: *
155: * @param pObject the message Object
156: */
157: public final void debug(Object pObject) {
158: getLogger().log(FQCN, Level.DEBUG, pObject, null);
159: }
160:
161: /**
162: * generate a message for loglevel INFO
163: *
164: * @param pObject the message Object
165: */
166: public final void info(Object pObject) {
167: getLogger().log(FQCN, Level.INFO, pObject, null);
168: }
169:
170: /**
171: * generate a message for loglevel WARN
172: *
173: * @param pObject the message Object
174: */
175: public final void warn(Object pObject) {
176: getLogger().log(FQCN, Level.WARN, pObject, null);
177: }
178:
179: /**
180: * generate a message for loglevel ERROR
181: *
182: * @param pObject the message Object
183: */
184: public final void error(Object pObject) {
185: getLogger().log(FQCN, Level.ERROR, pObject, null);
186: }
187:
188: /**
189: * generate a message for loglevel FATAL
190: *
191: * @param pObject the message Object
192: */
193: public final void fatal(Object pObject) {
194: getLogger().log(FQCN, Level.FATAL, pObject, null);
195: }
196:
197: public void debug(Object message, Throwable obj) {
198: getLogger().log(FQCN, Level.DEBUG, message, obj);
199: }
200:
201: public void error(Object message, Throwable obj) {
202: getLogger().log(FQCN, Level.ERROR, message, obj);
203: }
204:
205: public void fatal(Object message, Throwable obj) {
206: getLogger().log(FQCN, Level.FATAL, message, obj);
207: }
208:
209: public void info(Object message, Throwable obj) {
210: getLogger().log(FQCN, Level.INFO, message, obj);
211: }
212:
213: public void warn(Object message, Throwable obj) {
214: getLogger().log(FQCN, Level.WARN, message, obj);
215: }
216:
217: public void safeDebug(String message, Object obj) {
218: if (Level.DEBUG.isGreaterOrEqual(getLevel())) {
219: String toString = null;
220: if (obj != null) {
221: try {
222: toString = obj.toString();
223: } catch (Throwable t) {
224: toString = "BAD toString() impl for "
225: + obj.getClass().getName();
226: }
227: }
228: debug(message + " : " + toString);
229: }
230: }
231:
232: public void safeDebug(String message, Object obj,
233: Throwable throwable) {
234: if (Level.DEBUG.isGreaterOrEqual(getLevel())) {
235: String toString = null;
236: if (obj != null) {
237: try {
238: toString = obj.toString();
239: } catch (Throwable t) {
240: toString = "BAD toString() impl for "
241: + obj.getClass().getName();
242: }
243: }
244: debug(message + " : " + toString, throwable);
245: }
246: }
247:
248: public void safeInfo(String message, Object obj) {
249: if (Level.INFO.isGreaterOrEqual(getLevel())) {
250: String toString = null;
251: if (obj != null) {
252: try {
253: toString = obj.toString();
254: } catch (Throwable t) {
255: toString = "BAD toString() impl for "
256: + obj.getClass().getName();
257: }
258: }
259: info(message + " : " + toString);
260: }
261: }
262:
263: public void safeInfo(String message, Object obj, Throwable throwable) {
264: if (Level.INFO.isGreaterOrEqual(getLevel())) {
265: String toString = null;
266: if (obj != null) {
267: try {
268: toString = obj.toString();
269: } catch (Throwable t) {
270: toString = "BAD toString() impl for "
271: + obj.getClass().getName();
272: }
273: }
274: info(message + " : " + toString, throwable);
275: }
276: }
277:
278: public void safeWarn(String message, Object obj) {
279: if (Level.WARN.isGreaterOrEqual(getLevel())) {
280: String toString;
281: try {
282: toString = obj.toString();
283: } catch (Throwable t) {
284: toString = "BAD toString() impl for "
285: + obj.getClass().getName();
286: }
287: warn(message + " : " + toString);
288: }
289: }
290:
291: public void safeWarn(String message, Object obj, Throwable throwable) {
292: if (Level.WARN.isGreaterOrEqual(getLevel())) {
293: String toString;
294: try {
295: toString = obj.toString();
296: } catch (Throwable t) {
297: toString = "BAD toString() impl for "
298: + obj.getClass().getName();
299: }
300: warn(message + " : " + toString, throwable);
301: }
302: }
303:
304: public void safeError(String message, Object obj) {
305: if (Level.ERROR.isGreaterOrEqual(getLevel())) {
306: String toString;
307: try {
308: toString = obj.toString();
309: } catch (Throwable t) {
310: toString = "BAD toString() impl for "
311: + obj.getClass().getName();
312: }
313: error(message + " : " + toString);
314: }
315: }
316:
317: public void safeError(String message, Object obj,
318: Throwable throwable) {
319: if (Level.ERROR.isGreaterOrEqual(getLevel())) {
320: String toString;
321: try {
322: toString = obj.toString();
323: } catch (Throwable t) {
324: toString = "BAD toString() impl for "
325: + obj.getClass().getName();
326: }
327: error(message + " : " + toString, throwable);
328: }
329: }
330:
331: public void safeFatal(String message, Object obj) {
332: if (Level.FATAL.isGreaterOrEqual(getLevel())) {
333: String toString;
334: try {
335: toString = obj.toString();
336: } catch (Throwable t) {
337: toString = "BAD toString() impl for "
338: + obj.getClass().getName();
339: }
340: fatal(message + " : " + toString);
341: }
342: }
343:
344: public void safeFatal(String message, Object obj,
345: Throwable throwable) {
346: if (Level.FATAL.isGreaterOrEqual(getLevel())) {
347: String toString;
348: try {
349: toString = obj.toString();
350: } catch (Throwable t) {
351: toString = "BAD toString() impl for "
352: + obj.getClass().getName();
353: }
354: fatal(message + " : " + toString, throwable);
355: }
356: }
357:
358: public boolean isDebugEnabled() {
359: return getLogger().isDebugEnabled();
360: }
361:
362: public boolean isEnabledFor(int priority) {
363: org.apache.log4j.Logger log4j = getLogger();
364: switch (priority) {
365: case Logger.DEBUG:
366: return log4j.isDebugEnabled();
367: case Logger.INFO:
368: return log4j.isInfoEnabled();
369: case Logger.WARN:
370: return log4j.isEnabledFor(org.apache.log4j.Priority.WARN);
371: case Logger.ERROR:
372: return log4j.isEnabledFor(org.apache.log4j.Priority.ERROR);
373: case Logger.FATAL:
374: return log4j.isEnabledFor(org.apache.log4j.Priority.FATAL);
375: }
376: return false;
377: }
378: }
|