View Javadoc
1   /**
2    *    Copyright 2009-2019 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.slf4j;
17  
18  import org.apache.ibatis.logging.Log;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.slf4j.Marker;
22  import org.slf4j.spi.LocationAwareLogger;
23  
24  /**
25   * @author Clinton Begin
26   * @author Eduardo Macarron
27   */
28  public class Slf4jImpl implements Log {
29  
30    private Log log;
31  
32    public Slf4jImpl(String clazz) {
33      Logger logger = LoggerFactory.getLogger(clazz);
34  
35      if (logger instanceof LocationAwareLogger) {
36        try {
37          // check for slf4j >= 1.6 method signature
38          logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class, Throwable.class);
39          log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
40          return;
41        } catch (SecurityException | NoSuchMethodException e) {
42          // fail-back to Slf4jLoggerImpl
43        }
44      }
45  
46      // Logger is not LocationAwareLogger or slf4j version < 1.6
47      log = new Slf4jLoggerImpl(logger);
48    }
49  
50    @Override
51    public boolean isDebugEnabled() {
52      return log.isDebugEnabled();
53    }
54  
55    @Override
56    public boolean isTraceEnabled() {
57      return log.isTraceEnabled();
58    }
59  
60    @Override
61    public void error(String s, Throwable e) {
62      log.error(s, e);
63    }
64  
65    @Override
66    public void error(String s) {
67      log.error(s);
68    }
69  
70    @Override
71    public void debug(String s) {
72      log.debug(s);
73    }
74  
75    @Override
76    public void trace(String s) {
77      log.trace(s);
78    }
79  
80    @Override
81    public void warn(String s) {
82      log.warn(s);
83    }
84  
85  }