如何使用slf4j获取liquibase? [英] How to get liquibase to log using slf4j?

查看:151
本文介绍了如何使用slf4j获取liquibase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多不确定如何修复将liquibase记录到控制台或文件中。

A lot of people are unsure how to fix logging for liquibase, either to the console or file.

是否可以将liquibase登录到slf4j?

Is it possible to make liquibase log to slf4j?

推荐答案

有,但它是一个有点模糊。引用使用SLF4J和Log4J修复liquibase日志记录

There is, but it is a little bit obscure. Quoting 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.本身,它们仍然无法路由任何东西。

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.

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

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

如果您希望自己这样做,还有艰难之路

If you wish to do this yourself, there's also 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,所以这可能是一个更成熟的实现,加上它的开源。

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 Logger class 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.

这篇关于如何使用slf4j获取liquibase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆