Java 如何让 liquibase 使用 slf4j 登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20880783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:59:01  来源:igfitidea点击:

How to get liquibase to log using slf4j?

javalogginglog4jslf4jliquibase

提问by Benny Bottema

A lot of peopleare unsure howto fixlogging for liquibase, either to the console or file.

很多不清楚如何修复日志记录liquibase,或者到控制台或文件。

Is it possible to make liquibase log to slf4j?

是否可以将 liquibase 日志记录到 slf4j?

采纳答案by Benny Bottema

There is, but it is a little bit obscure. Quoting Fixing liquibase logging with SLF4J and Log4J:

有,但有点晦涩。使用 SLF4J 和 Log4J引用Fixing liquibase 日志记录

There's The Easy Way, by dropping in a dependency:

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>

Now the first two are your everyday logging frameworks (slf4j api and log4j implementation). These are in addition to your standard log4j dependency, as all they do is route to the physical logging framework. Without log4j/logback/etc. itself, they still can't route anything.

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

The last one however, is an interesting one, as it provides a single class in a specific package that liquibase will scan for Loggerimplementations. It's open source, by Matt Bertolini, so you can find it on GitHub.

然而,最后一个很有趣,因为它在特定包中提供了一个类,liquibase 将扫描Logger实现。它是开源的,由 Matt Bertolini 提供,因此您可以在 GitHub 上找到它

If you wish to do this yourself, there's also The Hard Way:

如果你想自己做这件事,还有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;
    }
}

This implementation works, but should only be used as an example. For example, I'm not using Liquibase's names to require a logging, but use this Loggerclass itself instead. Matt's versions does some null-checks as well, so that's probably a more mature implementation to use, plus it's open source.

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

回答by Waheed

I am not very sure about your requirement but what I understand is that You want to logged all liquibase log using SLF4J API. If I am right then I guess you can able to do it.

我不太确定您的要求,但我的理解是您想使用 SLF4J API 记录所有 liquibase 日志。如果我是对的,那么我想你可以做到。

First add following dependency into your pom.xml file :

首先将以下依赖项添加到您的 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>

and into your logback.xml file, Add logger for liquibase and set LEVEL as per your requirement.

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

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

回答by Waheed

I tried the same in my application and seems like it is working fine. I can see the liquibase log into my log file.

我在我的应用程序中尝试了相同的方法,看起来它工作正常。我可以看到 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

回答by vrogach

Here is my recipe to make liquibase 3.5.3 log into file under windows when running from command line. It doesn't use exactly 'slf4j' but solves the problem of getting log files of db updates, by making liquibase use java.util.logging.

这是我从命令行运行时使 liquibase 3.5.3 登录到 windows 下文件的方法。它并不完全使用“slf4j”,而是通过使 liquibase 使用 java.util.logging 来解决获取数据库更新日志文件的问题。

1) get the liquibase-javalogger-3.0.jar from here https://github.com/liquibase/liquibase-javalogger/releases/

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

2) put it to %LIQUIBASE_HOME%/lib directory

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

3) create logger.properties file with the following content:

3) 创建 logger.properties 文件,内容如下:

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

4) add java option to liquibase (for example via set JAVA_OPTS=...):

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

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

Example of my liquibase wrapper batch file :

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

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

UPDATE:

更新:

I've switched to new liquibase version which is using logback by default. For liquibase 3.6.2 use the following setup to run from windows command line:

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

1) Ensure slfj is reachable in java classpath. Put the slf4j-api-1.7.25.jar in liquibase/lib folder. The jar file can be found in the official slfj distribution package: https://www.slf4j.org/download.html

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

2) Set logback configuration file path parameter:

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

JAVA_OPTS=-Dlogback.configurationFile=logback.xml

3) Add logback.xml configuration file. Example is here: https://www.mkyong.com/logging/logback-xml-example/

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