View Javadoc
1   /*
2    *    Copyright 2006-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * Factory for creating loggers.
28   *
29   * @author Jeff Butler
30   */
31  public class LogFactory {
32      private static AbstractLogFactory theFactory;
33      public static final String MARKER = "MYBATIS-GENERATOR"; //$NON-NLS-1$
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", //$NON-NLS-1$
51                      clazz.getName(), t.getMessage()), t);
52          }
53      }
54  
55      /**
56       * This method will switch the logging implementation to Java native
57       * logging. This is useful in situations where you want to use Java native
58       * logging to log activity but Log4J is on the classpath. Note that this
59       * method is only effective for log classes obtained after calling this
60       * method. If you intend to use this method you should call it before
61       * calling any other method.
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                  // ignore
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."); //$NON-NLS-1$ //$NON-NLS-2$
102             }
103             theFactory = factory;
104         } catch (Throwable t) {
105             throw new LogException("Error setting Log implementation.  Cause: " + t.getMessage(), t); //$NON-NLS-1$
106         }
107     }
108 }