View Javadoc
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  
18  import org.apache.ibatis.logging.Log;
19  import org.apache.ibatis.logging.LogFactory;
20  import org.apache.logging.log4j.Level;
21  import org.apache.logging.log4j.Marker;
22  import org.apache.logging.log4j.MarkerManager;
23  import org.apache.logging.log4j.message.Message;
24  import org.apache.logging.log4j.message.SimpleMessage;
25  import org.apache.logging.log4j.spi.AbstractLogger;
26  import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;
27  
28  /**
29   * @author Eduardo Macarron
30   */
31  public class Log4j2AbstractLoggerImpl implements Log {
32  
33    private static final Marker MARKER = MarkerManager.getMarker(LogFactory.MARKER);
34  
35    private static final String FQCN = Log4j2Impl.class.getName();
36  
37    private final ExtendedLoggerWrapper log;
38  
39    public Log4j2AbstractLoggerImpl(AbstractLogger abstractLogger) {
40      log = new ExtendedLoggerWrapper(abstractLogger, abstractLogger.getName(), abstractLogger.getMessageFactory());
41    }
42  
43    @Override
44    public boolean isDebugEnabled() {
45      return log.isDebugEnabled();
46    }
47  
48    @Override
49    public boolean isTraceEnabled() {
50      return log.isTraceEnabled();
51    }
52  
53    @Override
54    public void error(String s, Throwable e) {
55      log.logIfEnabled(FQCN, Level.ERROR, MARKER, (Message) new SimpleMessage(s), e);
56    }
57  
58    @Override
59    public void error(String s) {
60      log.logIfEnabled(FQCN, Level.ERROR, MARKER, (Message) new SimpleMessage(s), null);
61    }
62  
63    @Override
64    public void debug(String s) {
65      log.logIfEnabled(FQCN, Level.DEBUG, MARKER, (Message) new SimpleMessage(s), null);
66    }
67  
68    @Override
69    public void trace(String s) {
70      log.logIfEnabled(FQCN, Level.TRACE, MARKER, (Message) new SimpleMessage(s), null);
71    }
72  
73    @Override
74    public void warn(String s) {
75      log.logIfEnabled(FQCN, Level.WARN, MARKER, (Message) new SimpleMessage(s), null);
76    }
77  
78  }