/
LogFactory.java
148 lines (129 loc) · 4.81 KB
/
LogFactory.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.logging;
/**
* A minimal incarnation of Apache Commons Logging's {@code LogFactory} API,
* providing just the common {@link Log} lookup methods. This is inspired
* by the JCL-over-SLF4J bridge and should be source as well as binary
* compatible with all common use of the Commons Logging API (in particular:
* with {@code LogFactory.getLog(Class/String)} field initializers).
*
* <p>This implementation does not support Commons Logging's original provider
* detection. It rather only checks for the presence of the Log4j 2.x API
* and the SLF4J 1.7 API in the Spring Framework classpath, falling back to
* {@code java.util.logging} if none of the two is available. In that sense,
* it works as a replacement for the Log4j 2 Commons Logging bridge as well as
* the JCL-over-SLF4J bridge, both of which become irrelevant for Spring-based
* setups as a consequence (with no need for manual excludes of the standard
* Commons Logging API jar anymore either). Furthermore, for simple setups
* without an external logging provider, Spring does not require any extra jar
* on the classpath anymore since this embedded log factory automatically
* delegates to {@code java.util.logging} in such a scenario.
*
* <p><b>Note that this Commons Logging variant is only meant to be used for
* infrastructure logging purposes in the core framework and in extensions.</b>
* It also serves as a common bridge for third-party libraries using the
* Commons Logging API, e.g. Apache HttpClient, and HtmlUnit, bringing
* them into the same consistent arrangement without any extra bridge jars.
*
* <p><b>For logging need in application code, prefer direct use of Log4j 2.x
* or SLF4J or {@code java.util.logging}.</b> Simply put Log4j 2.x or Logback
* (or another SLF4J provider) onto your classpath, without any extra bridges,
* and let the framework auto-adapt to your choice.
*
* @author Juergen Hoeller (for the {@code spring-jcl} variant)
* @since 5.0
*/
public abstract class LogFactory {
/**
* Convenience method to return a named logger.
* @param clazz containing Class from which a log name will be derived
*/
public static Log getLog(Class<?> clazz) {
return getLog(clazz.getName());
}
/**
* Convenience method to return a named logger.
* @param name logical name of the <code>Log</code> instance to be returned
*/
public static Log getLog(String name) {
return LogAdapter.createLog(name);
}
/**
* This method only exists for compatibility with unusual Commons Logging API
* usage like e.g. {@code LogFactory.getFactory().getInstance(Class/String)}.
* @see #getInstance(Class)
* @see #getInstance(String)
* @deprecated in favor of {@link #getLog(Class)}/{@link #getLog(String)}
*/
@Deprecated
public static LogFactory getFactory() {
return new LogFactory() {};
}
/**
* Convenience method to return a named logger.
* <p>This variant just dispatches straight to {@link #getLog(Class)}.
* @param clazz containing Class from which a log name will be derived
* @deprecated in favor of {@link #getLog(Class)}
*/
@Deprecated
public Log getInstance(Class<?> clazz) {
return getLog(clazz);
}
/**
* Convenience method to return a named logger.
* <p>This variant just dispatches straight to {@link #getLog(String)}.
* @param name logical name of the <code>Log</code> instance to be returned
* @deprecated in favor of {@link #getLog(String)}
*/
@Deprecated
public Log getInstance(String name) {
return getLog(name);
}
// Just in case some code happens to call uncommon Commons Logging methods...
@Deprecated
public Object getAttribute(String name) {
return null;
}
@Deprecated
public String[] getAttributeNames() {
return new String[0];
}
@Deprecated
public void removeAttribute(String name) {
// do nothing
}
@Deprecated
public void setAttribute(String name, Object value) {
// do nothing
}
@Deprecated
public void release() {
// do nothing
}
@Deprecated
public static void release(ClassLoader classLoader) {
// do nothing
}
@Deprecated
public static void releaseAll() {
// do nothing
}
@Deprecated
public static String objectId(Object o) {
return (o == null ? "null" : o.getClass().getName() + "@" + System.identityHashCode(o));
}
}