001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package liquibase.ext.logging.log4j2;
018
019import liquibase.logging.core.AbstractLogger;
020
021import org.apache.logging.log4j.Level;
022import org.apache.logging.log4j.LogManager;
023import org.apache.logging.log4j.spi.ExtendedLogger;
024
025/**
026 * Logs Liquibase messages to Log4j 2.x.
027 * <p>
028 * This class must be in the {@code liquibase} package in order for the Liquibase plugin discovery mechanism to work.
029 * </p>
030 */
031public class Log4j2Logger extends AbstractLogger {
032
033    private static final String FQCN = Log4j2Logger.class.getName();
034
035    private ExtendedLogger logger;
036
037    @Override
038    public void debug(final String message) {
039        logger.logIfEnabled(FQCN, Level.DEBUG, null, buildMessage(message));
040    }
041
042    @Override
043    public void debug(final String message, final Throwable e) {
044        logger.logIfEnabled(FQCN, Level.DEBUG, null, buildMessage(message), e);
045    }
046
047    @Override
048    public int getPriority() {
049        return PRIORITY_DATABASE;
050    }
051
052    @Override
053    public void info(final String message) {
054        logger.logIfEnabled(FQCN, Level.INFO, null, buildMessage(message));
055    }
056
057    @Override
058    public void info(final String message, final Throwable e) {
059        logger.logIfEnabled(FQCN, Level.INFO, null, buildMessage(message), e);
060    }
061
062    @Override
063    public void setLogLevel(final String logLevel, final String logFile) {
064        setLogLevel(logLevel);
065        // ignore logFile
066    }
067
068    @Override
069    public void setName(final String name) {
070        logger = LogManager.getContext(false).getLogger(name);
071    }
072
073    @Override
074    public void severe(final String message) {
075        logger.logIfEnabled(FQCN, Level.ERROR, null, buildMessage(message));
076    }
077
078    @Override
079    public void severe(final String message, final Throwable e) {
080        logger.logIfEnabled(FQCN, Level.ERROR, null, buildMessage(message), e);
081    }
082
083    @Override
084    public void warning(final String message) {
085        logger.logIfEnabled(FQCN, Level.WARN, null, buildMessage(message));
086    }
087
088    @Override
089    public void warning(final String message, final Throwable e) {
090        logger.logIfEnabled(FQCN, Level.WARN, null, buildMessage(message), e);
091    }
092
093}