View Javadoc
1   /*
2    * Copyright 2004-2025 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 com.ibatis.common.logging;
17  
18  import com.ibatis.common.resources.Resources;
19  
20  import java.lang.reflect.Constructor;
21  
22  /**
23   * A factory for creating Log objects.
24   */
25  public class LogFactory {
26  
27    /** The log constructor. */
28    private static Constructor logConstructor;
29  
30    static {
31      tryImplementation("org.apache.commons.logging.LogFactory",
32          "com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl");
33      tryImplementation("org.apache.log4j.Logger", "com.ibatis.common.logging.log4j.Log4jImpl");
34      tryImplementation("java.util.logging.Logger", "com.ibatis.common.logging.jdk14.Jdk14LoggingImpl");
35      tryImplementation("java.lang.Object", "com.ibatis.common.logging.nologging.NoLoggingImpl");
36    }
37  
38    /**
39     * Try implementation.
40     *
41     * @param testClassName
42     *          the test class name
43     * @param implClassName
44     *          the impl class name
45     */
46    private static void tryImplementation(String testClassName, String implClassName) {
47      if (logConstructor == null) {
48        try {
49          Resources.classForName(testClassName);
50          Class implClass = Resources.classForName(implClassName);
51          logConstructor = implClass.getConstructor(Class.class);
52        } catch (Throwable t) {
53        }
54      }
55    }
56  
57    /**
58     * Gets the log.
59     *
60     * @param aClass
61     *          the a class
62     *
63     * @return the log
64     */
65    public static Log getLog(Class aClass) {
66      try {
67        return (Log) logConstructor.newInstance(aClass);
68      } catch (Throwable t) {
69        throw new RuntimeException("Error creating logger for class " + aClass + ".  Cause: " + t, t);
70      }
71    }
72  
73    /**
74     * This method will switch the logging implementation to Log4J if Log4J is available on the classpath. This is useful
75     * in situations where you want to use Log4J to log iBATIS activity but commons logging is on the classpath. Note that
76     * this method is only effective for log classes obtained after calling this method. If you intend to use this method
77     * you should call it before calling any other iBATIS method.
78     */
79    public static synchronized void selectLog4JLogging() {
80      try {
81        Resources.classForName("org.apache.log4j.Logger");
82        Class implClass = Resources.classForName("com.ibatis.common.logging.log4j.Log4jImpl");
83        logConstructor = implClass.getConstructor(Class.class);
84      } catch (Throwable t) {
85      }
86    }
87  
88    /**
89     * This method will switch the logging implementation to Java native logging if you are running in JRE 1.4 or above.
90     * This is useful in situations where you want to use Java native logging to log iBATIS activity but commons logging
91     * or Log4J is on the classpath. Note that this method is only effective for log classes obtained after calling this
92     * method. If you intend to use this method you should call it before calling any other iBATIS method.
93     */
94    public static synchronized void selectJavaLogging() {
95      try {
96        Resources.classForName("java.util.logging.Logger");
97        Class implClass = Resources.classForName("com.ibatis.common.logging.jdk14.Jdk14LoggingImpl");
98        logConstructor = implClass.getConstructor(Class.class);
99      } catch (Throwable t) {
100     }
101   }
102 }