View Javadoc
1   /**
2    *    Copyright 2009-2015 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(new Runnable() {
35        @Override
36        public void run() {
37          useSlf4jLogging();
38        }
39      });
40      tryImplementation(new Runnable() {
41        @Override
42        public void run() {
43          useCommonsLogging();
44        }
45      });
46      tryImplementation(new Runnable() {
47        @Override
48        public void run() {
49          useLog4J2Logging();
50        }
51      });
52      tryImplementation(new Runnable() {
53        @Override
54        public void run() {
55          useLog4JLogging();
56        }
57      });
58      tryImplementation(new Runnable() {
59        @Override
60        public void run() {
61          useJdkLogging();
62        }
63      });
64      tryImplementation(new Runnable() {
65        @Override
66        public void run() {
67          useNoLogging();
68        }
69      });
70    }
71  
72    private LogFactory() {
73      // disable construction
74    }
75  
76    public static Log getLog(Class<?> aClass) {
77      return getLog(aClass.getName());
78    }
79  
80    public static Log getLog(String logger) {
81      try {
82        return logConstructor.newInstance(new Object[] { logger });
83      } catch (Throwable t) {
84        throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
85      }
86    }
87  
88    public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
89      setImplementation(clazz);
90    }
91  
92    public static synchronized void useSlf4jLogging() {
93      setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
94    }
95  
96    public static synchronized void useCommonsLogging() {
97      setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
98    }
99  
100   public static synchronized void useLog4JLogging() {
101     setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
102   }
103 
104   public static synchronized void useLog4J2Logging() {
105     setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
106   }
107 
108   public static synchronized void useJdkLogging() {
109     setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
110   }
111 
112   public static synchronized void useStdOutLogging() {
113     setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
114   }
115 
116   public static synchronized void useNoLogging() {
117     setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
118   }
119 
120   private static void tryImplementation(Runnable runnable) {
121     if (logConstructor == null) {
122       try {
123         runnable.run();
124       } catch (Throwable t) {
125         // ignore
126       }
127     }
128   }
129 
130   private static void setImplementation(Class<? extends Log> implClass) {
131     try {
132       Constructor<? extends Log> candidate = implClass.getConstructor(new Class[] { String.class });
133       Log log = candidate.newInstance(new Object[] { LogFactory.class.getName() });
134       if (log.isDebugEnabled()) {
135         log.debug("Logging initialized using '" + implClass + "' adapter.");
136       }
137       logConstructor = candidate;
138     } catch (Throwable t) {
139       throw new LogException("Error setting Log implementation.  Cause: " + t, t);
140     }
141   }
142 
143 }