View Javadoc
1   /**
2    *    Copyright 2009-2019 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<?> 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          // ignore
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 }