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(LogFactory::useSlf4jLogging);
35 tryImplementation(LogFactory::useCommonsLogging);
36 tryImplementation(LogFactory::useLog4J2Logging);
37 tryImplementation(LogFactory::useLog4JLogging);
38 tryImplementation(LogFactory::useJdkLogging);
39 tryImplementation(LogFactory::useNoLogging);
40 }
41
42 private LogFactory() {
43
44 }
45
46 public static Log getLog(Class<?> clazz) {
47 return getLog(clazz.getName());
48 }
49
50 public static Log getLog(String logger) {
51 try {
52 return logConstructor.newInstance(logger);
53 } catch (Throwable t) {
54 throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t);
55 }
56 }
57
58 public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
59 setImplementation(clazz);
60 }
61
62 public static synchronized void useSlf4jLogging() {
63 setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
64 }
65
66 public static synchronized void useCommonsLogging() {
67 setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
68 }
69
70
71
72
73 @Deprecated
74 public static synchronized void useLog4JLogging() {
75 setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
76 }
77
78 public static synchronized void useLog4J2Logging() {
79 setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
80 }
81
82 public static synchronized void useJdkLogging() {
83 setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
84 }
85
86 public static synchronized void useStdOutLogging() {
87 setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
88 }
89
90 public static synchronized void useNoLogging() {
91 setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
92 }
93
94 private static void tryImplementation(Runnable runnable) {
95 if (logConstructor == null) {
96 try {
97 runnable.run();
98 } catch (Throwable t) {
99
100 }
101 }
102 }
103
104 private static void setImplementation(Class<? extends Log> implClass) {
105 try {
106 Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
107 Log log = candidate.newInstance(LogFactory.class.getName());
108 if (log.isDebugEnabled()) {
109 log.debug("Logging initialized using '" + implClass + "' adapter.");
110 }
111 logConstructor = candidate;
112 } catch (Throwable t) {
113 throw new LogException("Error setting Log implementation. Cause: " + t, t);
114 }
115 }
116
117 }