View Javadoc
1   /*
2    *    Copyright 2006-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.mybatis.generator.logging.slf4j;
17  
18  import org.mybatis.generator.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  public class Slf4jImpl implements Log {
25  
26      private Log log;
27  
28      public Slf4jImpl(Class<?> clazz) {
29          Logger logger = LoggerFactory.getLogger(clazz);
30  
31          if (logger instanceof LocationAwareLogger) {
32              try {
33                  // check for slf4j >= 1.6 method signature
34                  logger.getClass().getMethod("log", Marker.class, String.class, int.class, //$NON-NLS-1$
35                          String.class, Object[].class, Throwable.class);
36                  log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
37                  return;
38              } catch (SecurityException | NoSuchMethodException e) {
39                  // fail-back to Slf4jLoggerImpl
40              }
41          }
42  
43          // Logger is not LocationAwareLogger or slf4j version < 1.6
44          log = new Slf4jLoggerImpl(logger);
45      }
46  
47      @Override
48      public boolean isDebugEnabled() {
49          return log.isDebugEnabled();
50      }
51  
52      @Override
53      public void error(String s, Throwable e) {
54          log.error(s, e);
55      }
56  
57      @Override
58      public void error(String s) {
59          log.error(s);
60      }
61  
62      @Override
63      public void debug(String s) {
64          log.debug(s);
65      }
66  
67      @Override
68      public void warn(String s) {
69          log.warn(s);
70      }
71  
72      @Override
73      public void info(String s) {
74          log.info(s);
75      }
76  }