Slf4jImpl.java

  1. /*
  2.  *    Copyright 2009-2023 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.slf4j;

  17. import org.apache.ibatis.logging.Log;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.slf4j.Marker;
  21. import org.slf4j.spi.LocationAwareLogger;

  22. /**
  23.  * @author Clinton Begin
  24.  * @author Eduardo Macarron
  25.  */
  26. public class Slf4jImpl implements Log {

  27.   private Log log;

  28.   public Slf4jImpl(String clazz) {
  29.     Logger logger = LoggerFactory.getLogger(clazz);

  30.     if (logger instanceof LocationAwareLogger) {
  31.       try {
  32.         // check for slf4j >= 1.6 method signature
  33.         logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class,
  34.             Throwable.class);
  35.         log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
  36.         return;
  37.       } catch (SecurityException | NoSuchMethodException e) {
  38.         // fail-back to Slf4jLoggerImpl
  39.       }
  40.     }

  41.     // Logger is not LocationAwareLogger or slf4j version < 1.6
  42.     log = new Slf4jLoggerImpl(logger);
  43.   }

  44.   @Override
  45.   public boolean isDebugEnabled() {
  46.     return log.isDebugEnabled();
  47.   }

  48.   @Override
  49.   public boolean isTraceEnabled() {
  50.     return log.isTraceEnabled();
  51.   }

  52.   @Override
  53.   public void error(String s, Throwable e) {
  54.     log.error(s, e);
  55.   }

  56.   @Override
  57.   public void error(String s) {
  58.     log.error(s);
  59.   }

  60.   @Override
  61.   public void debug(String s) {
  62.     log.debug(s);
  63.   }

  64.   @Override
  65.   public void trace(String s) {
  66.     log.trace(s);
  67.   }

  68.   @Override
  69.   public void warn(String s) {
  70.     log.warn(s);
  71.   }

  72. }