/
CommonsLoggerFactory.java
85 lines (78 loc) · 2.79 KB
/
CommonsLoggerFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2007-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.util.logging;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Jdk14Logger;
/**
* A factory for loggers that redirect all Java logging events to the Apache's <A
* HREF="http://jakarta.apache.org/commons/logging/">Commons-logging</A> framework.
*
* @since 2.4
* @source $URL$
* @version $Id$
* @author Martin Desruisseaux
*/
public class CommonsLoggerFactory extends LoggerFactory<Log> {
/** The unique instance of this factory. */
private static CommonsLoggerFactory factory;
/**
* Constructs a default factory.
*
* @throws NoClassDefFoundError if Apache's {@code Log} class was not found on the classpath.
*/
protected CommonsLoggerFactory() throws NoClassDefFoundError {
super(Log.class);
}
/**
* Returns the unique instance of this factory.
*
* @throws NoClassDefFoundError if Apache's {@code Log} class was not found on the classpath.
*/
public static synchronized CommonsLoggerFactory getInstance() throws NoClassDefFoundError {
if (factory == null) {
factory = new CommonsLoggerFactory();
}
return factory;
}
/**
* Returns the implementation to use for the logger of the specified name, or {@code null} if
* the logger would delegates to Java logging anyway.
*/
protected Log getImplementation(final String name) {
final Log log = LogFactory.getLog(name);
if (log instanceof Jdk14Logger) {
return null;
}
return log;
}
/** Wraps the specified {@linkplain #getImplementation implementation} in a Java logger. */
protected Logger wrap(String name, Log implementation) {
return new CommonsLogger(name, implementation);
}
/**
* Returns the {@linkplain #getImplementation implementation} wrapped by the specified logger,
* or {@code null} if none.
*/
protected Log unwrap(final Logger logger) {
if (logger instanceof CommonsLogger) {
return ((CommonsLogger) logger).logger;
}
return null;
}
}