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