View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.apache.ibatis.logging;
17  
18  import java.lang.reflect.Constructor;
19  
20  /**
21   * @author Clinton Begin
22   * @author Eduardo Macarron
23   */
24  public final class LogFactory {
25  
26    /**
27     * Marker to be used by logging implementations that support markers.
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      // disable construction
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     * @deprecated Since 3.5.9 - See https://github.com/mybatis/mybatis-3/issues/1223. This method will remove future.
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          // ignore
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 }