LogFactory.java

  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. import java.lang.reflect.Constructor;

  18. /**
  19.  * @author Clinton Begin
  20.  * @author Eduardo Macarron
  21.  */
  22. public final class LogFactory {

  23.   /**
  24.    * Marker to be used by logging implementations that support markers.
  25.    */
  26.   public static final String MARKER = "MYBATIS";

  27.   private static Constructor<? extends Log> logConstructor;

  28.   static {
  29.     tryImplementation(LogFactory::useSlf4jLogging);
  30.     tryImplementation(LogFactory::useCommonsLogging);
  31.     tryImplementation(LogFactory::useLog4J2Logging);
  32.     tryImplementation(LogFactory::useLog4JLogging);
  33.     tryImplementation(LogFactory::useJdkLogging);
  34.     tryImplementation(LogFactory::useNoLogging);
  35.   }

  36.   private LogFactory() {
  37.     // disable construction
  38.   }

  39.   public static Log getLog(Class<?> clazz) {
  40.     return getLog(clazz.getName());
  41.   }

  42.   public static Log getLog(String logger) {
  43.     try {
  44.       return logConstructor.newInstance(logger);
  45.     } catch (Throwable t) {
  46.       throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
  47.     }
  48.   }

  49.   public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
  50.     setImplementation(clazz);
  51.   }

  52.   public static synchronized void useSlf4jLogging() {
  53.     setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  54.   }

  55.   public static synchronized void useCommonsLogging() {
  56.     setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  57.   }

  58.   /**
  59.    * @deprecated Since 3.5.9 - See https://github.com/mybatis/mybatis-3/issues/1223. This method will remove future.
  60.    */
  61.   @Deprecated
  62.   public static synchronized void useLog4JLogging() {
  63.     setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
  64.   }

  65.   public static synchronized void useLog4J2Logging() {
  66.     setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  67.   }

  68.   public static synchronized void useJdkLogging() {
  69.     setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  70.   }

  71.   public static synchronized void useStdOutLogging() {
  72.     setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  73.   }

  74.   public static synchronized void useNoLogging() {
  75.     setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  76.   }

  77.   private static void tryImplementation(Runnable runnable) {
  78.     if (logConstructor == null) {
  79.       try {
  80.         runnable.run();
  81.       } catch (Throwable t) {
  82.         // ignore
  83.       }
  84.     }
  85.   }

  86.   private static void setImplementation(Class<? extends Log> implClass) {
  87.     try {
  88.       Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
  89.       Log log = candidate.newInstance(LogFactory.class.getName());
  90.       if (log.isDebugEnabled()) {
  91.         log.debug("Logging initialized using '" + implClass + "' adapter.");
  92.       }
  93.       logConstructor = candidate;
  94.     } catch (Throwable t) {
  95.       throw new LogException("Error setting Log implementation.  Cause: " + t, t);
  96.     }
  97.   }

  98. }