View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package liquibase.ext.logging.log4j2;
18  
19  import liquibase.logging.core.AbstractLogger;
20  
21  import org.apache.logging.log4j.Level;
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.spi.ExtendedLogger;
24  
25  /**
26   * Logs Liquibase messages to Log4j 2.x.
27   * <p>
28   * This class must be in the {@code liquibase} package in order for the Liquibase plugin discovery mechanism to work.
29   * </p>
30   */
31  public class Log4j2Logger extends AbstractLogger {
32  
33      private static final String FQCN = Log4j2Logger.class.getName();
34  
35      private ExtendedLogger logger;
36  
37      @Override
38      public void debug(final String message) {
39          logger.logIfEnabled(FQCN, Level.DEBUG, null, buildMessage(message));
40      }
41  
42      @Override
43      public void debug(final String message, final Throwable e) {
44          logger.logIfEnabled(FQCN, Level.DEBUG, null, buildMessage(message), e);
45      }
46  
47      @Override
48      public int getPriority() {
49          return PRIORITY_DATABASE;
50      }
51  
52      @Override
53      public void info(final String message) {
54          logger.logIfEnabled(FQCN, Level.INFO, null, buildMessage(message));
55      }
56  
57      @Override
58      public void info(final String message, final Throwable e) {
59          logger.logIfEnabled(FQCN, Level.INFO, null, buildMessage(message), e);
60      }
61  
62      @Override
63      public void setLogLevel(final String logLevel, final String logFile) {
64          setLogLevel(logLevel);
65          // ignore logFile
66      }
67  
68      @Override
69      public void setName(final String name) {
70          logger = LogManager.getContext(false).getLogger(name);
71      }
72  
73      @Override
74      public void severe(final String message) {
75          logger.logIfEnabled(FQCN, Level.ERROR, null, buildMessage(message));
76      }
77  
78      @Override
79      public void severe(final String message, final Throwable e) {
80          logger.logIfEnabled(FQCN, Level.ERROR, null, buildMessage(message), e);
81      }
82  
83      @Override
84      public void warning(final String message) {
85          logger.logIfEnabled(FQCN, Level.WARN, null, buildMessage(message));
86      }
87  
88      @Override
89      public void warning(final String message, final Throwable e) {
90          logger.logIfEnabled(FQCN, Level.WARN, null, buildMessage(message), e);
91      }
92  
93  }