01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2007, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.util.logging;
17:
18: import java.util.logging.Logger;
19:
20: /**
21: * A factory for loggers that redirect all Java logging events to the Apache's
22: * <A HREF="http://logging.apache.org/log4j">Log4J</A> framework.
23: *
24: * @since 2.4
25: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/logging/Log4JLoggerFactory.java $
26: * @version $Id: Log4JLoggerFactory.java 27891 2007-11-14 14:10:48Z desruisseaux $
27: * @author Martin Desruisseaux
28: */
29: public class Log4JLoggerFactory extends LoggerFactory {
30: /**
31: * The unique instance of this factory.
32: */
33: private static Log4JLoggerFactory factory;
34:
35: /**
36: * Constructs a default factory.
37: *
38: * @throws NoClassDefFoundError if Apache's {@code Log} class was not found on the classpath.
39: */
40: protected Log4JLoggerFactory() throws NoClassDefFoundError {
41: super (org.apache.log4j.Logger.class);
42: }
43:
44: /**
45: * Returns the unique instance of this factory.
46: *
47: * @throws NoClassDefFoundError if Apache's {@code Log} class was not found on the classpath.
48: */
49: public static synchronized Log4JLoggerFactory getInstance()
50: throws NoClassDefFoundError {
51: if (factory == null) {
52: factory = new Log4JLoggerFactory();
53: }
54: return factory;
55: }
56:
57: /**
58: * Returns the implementation to use for the logger of the specified name,
59: * or {@code null} if the logger would delegates to Java logging anyway.
60: */
61: protected Object getImplementation(final String name) {
62: return org.apache.log4j.Logger.getLogger(name);
63: }
64:
65: /**
66: * Wraps the specified {@linkplain #getImplementation implementation} in a Java logger.
67: */
68: protected Logger wrap(String name, Object implementation)
69: throws ClassCastException {
70: return new Log4JLogger(name,
71: (org.apache.log4j.Logger) implementation);
72: }
73:
74: /**
75: * Returns the {@linkplain #getImplementation implementation} wrapped by the specified logger,
76: * or {@code null} if none.
77: */
78: protected Object unwrap(final Logger logger) {
79: if (logger instanceof Log4JLogger) {
80: return ((Log4JLogger) logger).logger;
81: }
82: return null;
83: }
84: }
|