Flyway logging with Logback

2019-07-20 20:55发布

问题:

I have a small application configured to use SLF4J + Logback. I'm getting JUL output from Flyway which I'd like Logback to handle.

FlywayWrapper.java

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory;

public class FlywayWrapper {

    private static final Logger logger = LoggerFactory.getLogger(FlywayWrapper.class);

logback.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date{ISO8601} [%thread] %-5level %logger{0}: %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.foo.test" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

And I'm seeing console logging like so:

2014-09-01 12:53:47,405 [main] INFO  FlywayWrapper: Operation >> migrate
Sep 01, 2014 12:53:47 PM org.flywaydb.core.internal.dbsupport.DbSupportFactory createDbSupport
INFO: Database: jdbc:postgresql://localhost:5433/test (PostgreSQL 9.2)
Sep 01, 2014 12:53:47 PM org.flywaydb.core.internal.command.DbValidate validate
INFO: Validated 3 migrations (execution time 00:00.055s)

I've noticed this in org.flywaydb.core.internal.util.logging.LogFactory which is probably why JUL is being used.

public static Log getLog(Class<?> clazz) {
        if (logCreator == null) {
            FeatureDetector featureDetector = new FeatureDetector(Thread.currentThread().getContextClassLoader());
            if (featureDetector.isAndroidAvailable()) {
                logCreator = new AndroidLogCreator();
            } else if (featureDetector.isApacheCommonsLoggingAvailable()) {
                logCreator = new ApacheCommonsLogCreator();
            } else {
                logCreator = new JavaUtilLogCreator();
            }
        }

        return logCreator.createLogger(clazz);
    }

Any ideas on how I can get Flyway to use my logging backend?

回答1:

This will be supported as part of the 3.1 release: https://github.com/flyway/flyway/issues/834



回答2:

Flyway 2 does not recognise the presence of Logback, but it is easily configurable.

You need to call the static method com.googlecode.flyway.core.util.logging.LogFactory.setLogCreator from your code, before using Flyway, and give it your own implementation of com.googlecode.flyway.core.util.logging.Log interface, which should be a simple wrapper using Slf4j logger.

Something like this would work:

com.googlecode.flyway.core.util.logging.LogFactory.setLogCreator(
        new com.googlecode.flyway.core.util.logging.LogCreator() {
            @Override
            public com.googlecode.flyway.core.util.logging.Log createLogger(final Class<?> clazz) {
                return new com.googlecode.flyway.core.util.logging.Log() {
                    private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(clazz);
                    @Override
                    public void debug(String message) { logger.debug(message); }

                    @Override
                    public void info(String message) { logger.info(message); }

                    @Override
                    public void warn(String message) { logger.warn(message); }

                    @Override
                    public void error(String message) { logger.error(message); }

                    @Override
                    public void error(String message, Exception e) { logger.error(message, e); }
                };
            }
        });