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