Log4j2AbstractLoggerImpl.java

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

  17. import org.apache.ibatis.logging.Log;
  18. import org.apache.ibatis.logging.LogFactory;
  19. import org.apache.logging.log4j.Level;
  20. import org.apache.logging.log4j.Marker;
  21. import org.apache.logging.log4j.MarkerManager;
  22. import org.apache.logging.log4j.message.Message;
  23. import org.apache.logging.log4j.message.SimpleMessage;
  24. import org.apache.logging.log4j.spi.AbstractLogger;
  25. import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;

  26. /**
  27.  * @author Eduardo Macarron
  28.  */
  29. public class Log4j2AbstractLoggerImpl implements Log {

  30.   private static final Marker MARKER = MarkerManager.getMarker(LogFactory.MARKER);

  31.   private static final String FQCN = Log4j2Impl.class.getName();

  32.   private final ExtendedLoggerWrapper log;

  33.   public Log4j2AbstractLoggerImpl(AbstractLogger abstractLogger) {
  34.     log = new ExtendedLoggerWrapper(abstractLogger, abstractLogger.getName(), abstractLogger.getMessageFactory());
  35.   }

  36.   @Override
  37.   public boolean isDebugEnabled() {
  38.     return log.isDebugEnabled();
  39.   }

  40.   @Override
  41.   public boolean isTraceEnabled() {
  42.     return log.isTraceEnabled();
  43.   }

  44.   @Override
  45.   public void error(String s, Throwable e) {
  46.     log.logIfEnabled(FQCN, Level.ERROR, MARKER, (Message) new SimpleMessage(s), e);
  47.   }

  48.   @Override
  49.   public void error(String s) {
  50.     log.logIfEnabled(FQCN, Level.ERROR, MARKER, (Message) new SimpleMessage(s), null);
  51.   }

  52.   @Override
  53.   public void debug(String s) {
  54.     log.logIfEnabled(FQCN, Level.DEBUG, MARKER, (Message) new SimpleMessage(s), null);
  55.   }

  56.   @Override
  57.   public void trace(String s) {
  58.     log.logIfEnabled(FQCN, Level.TRACE, MARKER, (Message) new SimpleMessage(s), null);
  59.   }

  60.   @Override
  61.   public void warn(String s) {
  62.     log.logIfEnabled(FQCN, Level.WARN, MARKER, (Message) new SimpleMessage(s), null);
  63.   }

  64. }