1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.logging;
17
18 import static org.mybatis.generator.internal.util.messages.Messages.getString;
19
20 import org.mybatis.generator.logging.commons.JakartaCommonsLoggingLogFactory;
21 import org.mybatis.generator.logging.jdk14.Jdk14LoggingLogFactory;
22 import org.mybatis.generator.logging.log4j2.Log4j2LoggingLogFactory;
23 import org.mybatis.generator.logging.nologging.NoLoggingLogFactory;
24 import org.mybatis.generator.logging.slf4j.Slf4jLoggingLogFactory;
25
26
27
28
29
30
31 public class LogFactory {
32 private static AbstractLogFactory theFactory;
33 public static final String MARKER = "MYBATIS-GENERATOR";
34
35 static {
36 tryImplementation(new Slf4jLoggingLogFactory());
37 tryImplementation(new JakartaCommonsLoggingLogFactory());
38 tryImplementation(new Log4j2LoggingLogFactory());
39 tryImplementation(new Jdk14LoggingLogFactory());
40 tryImplementation(new NoLoggingLogFactory());
41 }
42
43 private LogFactory() {
44 }
45
46 public static Log getLog(Class<?> clazz) {
47 try {
48 return theFactory.getLog(clazz);
49 } catch (Exception t) {
50 throw new RuntimeException(getString("RuntimeError.21",
51 clazz.getName(), t.getMessage()), t);
52 }
53 }
54
55
56
57
58
59
60
61
62
63 public static synchronized void forceJavaLogging() {
64 setImplementation(new Jdk14LoggingLogFactory());
65 }
66
67 public static synchronized void forceSlf4jLogging() {
68 setImplementation(new Slf4jLoggingLogFactory());
69 }
70
71 public static synchronized void forceCommonsLogging() {
72 setImplementation(new JakartaCommonsLoggingLogFactory());
73 }
74
75 public static synchronized void forceLog4j2Logging() {
76 setImplementation(new Log4j2LoggingLogFactory());
77 }
78
79 public static synchronized void forceNoLogging() {
80 setImplementation(new NoLoggingLogFactory());
81 }
82
83 public static void setLogFactory(AbstractLogFactory logFactory) {
84 setImplementation(logFactory);
85 }
86
87 private static void tryImplementation(AbstractLogFactory factory) {
88 if (theFactory == null) {
89 try {
90 setImplementation(factory);
91 } catch (LogException e) {
92
93 }
94 }
95 }
96
97 private static void setImplementation(AbstractLogFactory factory) {
98 try {
99 Log log = factory.getLog(LogFactory.class);
100 if (log.isDebugEnabled()) {
101 log.debug("Logging initialized using '" + factory + "' adapter.");
102 }
103 theFactory = factory;
104 } catch (Throwable t) {
105 throw new LogException("Error setting Log implementation. Cause: " + t.getMessage(), t);
106 }
107 }
108 }