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<?> aClass) {
47 return getLog(aClass.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 public static synchronized void useLog4JLogging() {
71 setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
72 }
73
74 public static synchronized void useLog4J2Logging() {
75 setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
76 }
77
78 public static synchronized void useJdkLogging() {
79 setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
80 }
81
82 public static synchronized void useStdOutLogging() {
83 setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
84 }
85
86 public static synchronized void useNoLogging() {
87 setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
88 }
89
90 private static void tryImplementation(Runnable runnable) {
91 if (logConstructor == null) {
92 try {
93 runnable.run();
94 } catch (Throwable t) {
95
96 }
97 }
98 }
99
100 private static void setImplementation(Class<? extends Log> implClass) {
101 try {
102 Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
103 Log log = candidate.newInstance(LogFactory.class.getName());
104 if (log.isDebugEnabled()) {
105 log.debug("Logging initialized using '" + implClass + "' adapter.");
106 }
107 logConstructor = candidate;
108 } catch (Throwable t) {
109 throw new LogException("Error setting Log implementation. Cause: " + t, t);
110 }
111 }
112
113 }