2014-09-01 128 views
2

我有一個配置爲使用SLF4J + Logback的小應用程序。我從Flyway獲取JUL輸出,我希望Logback能夠處理這些輸出。Flyway Logback的日誌記錄

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> 

而且我看到控制檯日誌記錄,像這樣:

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) 

我在org.flywaydb.core.internal.util.logging.LogFactory注意到這這可能是爲什麼JUL是正在使用。

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); 
    } 

有關我如何讓Flyway使用我的日誌記錄後端的任何想法?

回答

1

遷飛2不承認的logback的存在,但它很容易配置。

在使用Flyway之前,您需要從代碼中調用靜態方法com.googlecode.flyway.core.util.logging.LogFactory.setLogCreator,並給它自己的接口com.googlecode.flyway.core.util.logging.Log接口,它應該是使用Slf4j記錄器的簡單包裝。

像這樣的東西會工作:

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); } 
       }; 
      } 
     }); 
相關問題