1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.logging;
17
18 import java.lang.reflect.Constructor;
19
20
21
22
23
24 public final class LogFactory {
25
26
27
28
29 public static final String MARKER = "MYBATIS";
30
31 private static Constructor<? extends Log> logConstructor;
32
33 static {
34 tryImplementation(new Runnable() {
35 @Override
36 public void run() {
37 useSlf4jLogging();
38 }
39 });
40 tryImplementation(new Runnable() {
41 @Override
42 public void run() {
43 useCommonsLogging();
44 }
45 });
46 tryImplementation(new Runnable() {
47 @Override
48 public void run() {
49 useLog4J2Logging();
50 }
51 });
52 tryImplementation(new Runnable() {
53 @Override
54 public void run() {
55 useLog4JLogging();
56 }
57 });
58 tryImplementation(new Runnable() {
59 @Override
60 public void run() {
61 useJdkLogging();
62 }
63 });
64 tryImplementation(new Runnable() {
65 @Override
66 public void run() {
67 useNoLogging();
68 }
69 });
70 }
71
72 private LogFactory() {
73
74 }
75
76 public static Log getLog(Class<?> aClass) {
77 return getLog(aClass.getName());
78 }
79
80 public static Log getLog(String logger) {
81 try {
82 return logConstructor.newInstance(new Object[] { logger });
83 } catch (Throwable t) {
84 throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t);
85 }
86 }
87
88 public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
89 setImplementation(clazz);
90 }
91
92 public static synchronized void useSlf4jLogging() {
93 setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
94 }
95
96 public static synchronized void useCommonsLogging() {
97 setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
98 }
99
100 public static synchronized void useLog4JLogging() {
101 setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
102 }
103
104 public static synchronized void useLog4J2Logging() {
105 setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
106 }
107
108 public static synchronized void useJdkLogging() {
109 setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
110 }
111
112 public static synchronized void useStdOutLogging() {
113 setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
114 }
115
116 public static synchronized void useNoLogging() {
117 setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
118 }
119
120 private static void tryImplementation(Runnable runnable) {
121 if (logConstructor == null) {
122 try {
123 runnable.run();
124 } catch (Throwable t) {
125
126 }
127 }
128 }
129
130 private static void setImplementation(Class<? extends Log> implClass) {
131 try {
132 Constructor<? extends Log> candidate = implClass.getConstructor(new Class[] { String.class });
133 Log log = candidate.newInstance(new Object[] { LogFactory.class.getName() });
134 if (log.isDebugEnabled()) {
135 log.debug("Logging initialized using '" + implClass + "' adapter.");
136 }
137 logConstructor = candidate;
138 } catch (Throwable t) {
139 throw new LogException("Error setting Log implementation. Cause: " + t, t);
140 }
141 }
142
143 }