LogFactory.java

  1. /*
  2.  *    Copyright 2009-2024 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 org.apache.ibatis.logging;

  17. import java.lang.reflect.Constructor;
  18. import java.util.concurrent.locks.ReentrantLock;

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

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

  28.   private static final ReentrantLock lock = new ReentrantLock();
  29.   private static Constructor<? extends Log> logConstructor;

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

  38.   private LogFactory() {
  39.     // disable construction
  40.   }

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

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

  51.   public static void useCustomLogging(Class<? extends Log> clazz) {
  52.     setImplementation(clazz);
  53.   }

  54.   public static void useSlf4jLogging() {
  55.     setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  56.   }

  57.   public static void useCommonsLogging() {
  58.     setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  59.   }

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

  67.   public static void useLog4J2Logging() {
  68.     setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  69.   }

  70.   public static void useJdkLogging() {
  71.     setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  72.   }

  73.   public static void useStdOutLogging() {
  74.     setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  75.   }

  76.   public static void useNoLogging() {
  77.     setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  78.   }

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

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

  103. }