如何使用slf4j获取liquibase?

时间:2014-01-02 10:23:36

标签: java logging log4j slf4j liquibase

很多people都不确定howfix将liquibase记录到控制台或文件中。

是否可以将liquibase登录到slf4j?

4 个答案:

答案 0 :(得分:42)

有,但它有点模糊。引用Fixing liquibase logging with SLF4J and Log4J

The Easy Way ,通过删除依赖:

<!-- your own standard logging dependencies -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId><!-- or log4j2 or logback or whatever-->
    <version>1.7.5</version>
</dependency>

<!-- special dependency to fix liquibase's logging fetish -->
<dependency>
    <groupId>com.mattbertolini</groupId>
    <artifactId>liquibase-slf4j</artifactId>
    <version>1.2.1</version>
</dependency>

现在前两个是你的日常日志框架(slf4j api和log4j实现)。这些是您的标准log4j依赖项的补充,因为他们所做的只是路由到物理日志框架。没有log4j / logback / etc.本身,他们仍然无法路由任何东西。

然而,最后一个是有趣的,因为它在特定包中提供liquibase将扫描Logger实现的单个类。这是Matt Bertolini的开源,所以你可以find it on GitHub

如果您希望自己这样做,还有The Hard Way

package liquibase.ext.logging; // this is *very* important

import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.logging.core.AbstractLogger;

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

/**
 * Liquibase finds this class by itself by doing a custom component scan (sl4fj wasn't generic enough).
 */
public class LiquibaseLogger extends AbstractLogger {
    private static final Logger LOGGER = LoggerFactory.getLogger(LiquibaseLogger.class);
    private String name = "";

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void severe(String message) {
        LOGGER.error("{} {}", name, message);
    }

    @Override
    public void severe(String message, Throwable e) {
        LOGGER.error("{} {}", name, message, e);
    }

    @Override
    public void warning(String message) {
        LOGGER.warn("{} {}", name, message);
    }

    @Override
    public void warning(String message, Throwable e) {
        LOGGER.warn("{} {}", name, message, e);
    }

    @Override
    public void info(String message) {
        LOGGER.info("{} {}", name, message);
    }

    @Override
    public void info(String message, Throwable e) {
        LOGGER.info("{} {}", name, message, e);
    }

    @Override
    public void debug(String message) {
        LOGGER.debug("{} {}", name, message);
    }

    @Override
    public void debug(String message, Throwable e) {
        LOGGER.debug("{} {}", message, e);
    }

    @Override
    public void setLogLevel(String logLevel, String logFile) {
    }

    @Override
    public void setChangeLog(DatabaseChangeLog databaseChangeLog) {
    }

    @Override
    public void setChangeSet(ChangeSet changeSet) {
    }

    @Override
    public int getPriority() {
        return Integer.MAX_VALUE;
    }
}

此实现有效,但应仅用作示例。例如,我没有使用Liquibase的名称来要求记录,而是使用此Logger类本身。 Matt的版本也有一些 null -checks,所以这可能是一个更成熟的实现,加上它的开源。

答案 1 :(得分:4)

我不太确定您的要求,但据我所知,您希望使用SLF4J API记录所有liquibase日志。如果我是对的,那么我想你能够做到。

首先将以下依赖项添加到您的pom.xml文件中:

<dependency>
    <groupId>com.mattbertolini</groupId>
    <artifactId>liquibase-slf4j</artifactId>
    <version>1.2.1</version>
</dependency>

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.6</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.6.6</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.7</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.7</version>
    </dependency>

并在您的logback.xml文件中添加liquibase的记录器,并根据您的要求设置LEVEL。

  <logger name="liquibase" level="DEBUG" />

答案 2 :(得分:2)

这是我从命令行运行时使liquibase 3.5.3登录到windows下的文件的配方。 它没有完全使用'slf4j',但通过使liquibase使用java.util.logging解决了获取db更新日志文件的问题。

1)从这里获取liquibase-javalogger-3.0.jar https://github.com/liquibase/liquibase-javalogger/releases/

2)将它放到%LIQUIBASE_HOME%/ lib目录

3)使用以下内容创建logger.properties文件:

List1 = ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".",1,2,3,4,5,6,7,8,9,0)
List2 = (4,"R",5,"G","Z",3,2,"D","A","E","X","Y","U","I",6,"W",7,"O","V",8,"F","Q","L",0,"J",".","H",9,"C","B","N","S","P","M",1,"T","K")

while choice != EXIT:
    display_menu()

    choice = int(input("Enter a number between 1 and 3."))

    if choice == encrypt:
        encrypt_msg = str(input("Give me a Message to Encrypt Please"))
    elif choice == decrypt:
        result = 0
        decrypt_msg = str(input("Give me Your message to decrypt please."))

        print("Exiting...")
    else:
        Print("Error...Invalid Response")

4)向liquibase添加java选项(例如通过set JAVA_OPTS = ...):

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern=liquibase.log
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.append=true
#2018-04-28 17:29:44 INFO Example logging record
java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %5$s%6$s%n

liquibase包装器批处理文件的示例:

-Djava.util.logging.config.file=logger.properties

更新:

我已切换到默认使用logback的新liquibase版本。 对于liquibase 3.6.2,请使用以下设置从Windows命令行运行:

1)确保在java classpath中可以访问slfj。 将slf4j-api-1.7.25.jar放在liquibase / lib文件夹中。 jar文件可以在官方的slfj发行包中找到: https://www.slf4j.org/download.html

2)设置logback配置文件路径参数:

set username=%USER%
set password="%PASSWORD%"

set URL="jdbc:db2://mydbserver:50000/MYDB"

set JAVA_OPTS=-Djava.util.logging.config.file=db2/logger.properties

call liquibase.bat ^
    --driver="com.ibm.db2.jcc.DB2Driver" ^
    --defaultSchemaName=MYSCHEMA ^
    --liquibaseSchemaName=MYSCHEMA ^
    --changeLogFile=db2/changelog.xml ^
    --url=%URL% ^
    --username=%USER% ^
    --password="%PASSWORD%" ^
    --logLevel=debug

3)添加logback.xml配置文件。 示例在这里: https://www.mkyong.com/logging/logback-xml-example/

答案 3 :(得分:0)

我在我的应用程序中尝试了相同的操作,似乎工作正常。我可以看到liquibase登录到我的日志文件中。

2014-01-08 11:16:21,452 [main] DEBUG liquibase - Computed checksum for addColumn:[
    columns=[
        column:[
            name="IS_NEW"
            type="BIT"
        ]
    ]
    tableName="TENANT"
] as e2eb1f5cb8dcfca7d064223044d06de9
2014-01-08 11:16:21,452 [main] DEBUG liquibase - Computed checksum for 3:e2eb1f5cb8dcfca7d064223044d06de9: as 549852ffb531de4929ae433ff0be2742
2014-01-08 11:16:21,455 [main] DEBUG liquibase - Release Database Lock
2014-01-08 11:16:21,456 [main] DEBUG liquibase - Executing UPDATE database command: UPDATE `DATABASECHANGELOGLOCK` SET `LOCKED` = 0, `LOCKEDBY` = NULL, `LOCKGRANTED` = NULL WHERE `ID` = 1
2014-01-08 11:16:21,518 [main] INFO  liquibase - Successfully released change log lock