CommonsLogger等效于slf4j

时间:2012-12-21 10:00:26

标签: slf4j apache-commons-logging

我们正在从commons-logging迁移到slf4j。

我们正在使用org.apache.fop.apps.Driver类来生成PDF文件。现在,Driver类的setLogger()方法将org.apache.avalon.framework.logger.Logger作为参数。

CommonsLogger方法在org.apache.avalon.framework.logger包中提供,该包将commons-logging logger转换为org.apache.avalon.framework.logger.Logger。

请在下面找到示例代码:

org.apache.fop.apps.Driver driver = new Driver();
driver.setLogger(new CommonsLogger(logger, getClass().getName()));

现在,替换slf4j记录器应该是什么?

最好,我猜可以使用NullLogger()而不是CommonsLogger()。请告诉我是否有更好的解决方案。

2 个答案:

答案 0 :(得分:0)

我建议你创建一个带有SLF4J记录器的org.apache.avalon.framework.logger.Logger实现。与此处类似 - https://svn.ametys.org/trunk/runtime/trunk/main/kernel/src/org/ametys/runtime/util/SLF4JLogger.java

答案 1 :(得分:0)

不确定如何将fatal日志消息类型映射到SL4J,但这是有效的代码段:

import lombok.extern.slf4j.Slf4j;
import org.apache.avalon.framework.logger.Logger;

@Slf4j
public class SL4JLoggerAdapter implements Logger {
    public static final int LEVEL_DEBUG = 0;
    public static final int LEVEL_INFO = 1;
    public static final int LEVEL_WARN = 2;
    public static final int LEVEL_ERROR = 3;
    public static final int LEVEL_FATAL = 4;
    public static final int LEVEL_DISABLED = 5;

    private final int m_logLevel;

    public SL4JLoggerAdapter( final int logLevel )
    {
        m_logLevel = logLevel;
    }

    @Override
    public void debug(String message) {
        log.debug(message);
    }

    @Override
    public void debug(String message, Throwable throwable) {
        log.debug(message, throwable);
    }

    @Override
    public boolean isDebugEnabled() {
        return m_logLevel == LEVEL_DEBUG;
    }

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

    @Override
    public void info(String message, Throwable throwable) {
        log.info(message, throwable);
    }

    @Override
    public boolean isInfoEnabled() {
        return m_logLevel == LEVEL_INFO;
    }

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

    @Override
    public void warn(String message, Throwable throwable) {
        log.warn(message, throwable);
    }

    @Override
    public boolean isWarnEnabled() {
        return m_logLevel == LEVEL_WARN;

    }

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

    @Override
    public void error(String message, Throwable throwable) {
        log.error(message, throwable);
    }

    @Override
    public boolean isErrorEnabled() {
        return m_logLevel == LEVEL_ERROR;
    }

    @Override
    public void fatalError(String message) {
        log.trace(message);
    }

    @Override
    public void fatalError(String message, Throwable throwable) {
        log.trace(message, throwable);
    }

    @Override
    public boolean isFatalErrorEnabled() {
        return m_logLevel == LEVEL_FATAL;

    }

    @Override
    public Logger getChildLogger(String name) {
        return this;
    }
}