java – logback:两个appender,多个logger,不同的级别

我想在我的应用程序(Spring Integration),debug.log和main.log中有两个日志文件。我想在INFO级别运行main.log,在DEBUG级别运行debug.log。这是可以在appender上的过滤器。我想根据源来记录不同的级别到appender。换一种说法

<logger name="org.springframework" level="ERROR">
    <appender-ref ref="main" />
</logger>
<logger name="org.springframework" level="DEBUG">
    <appender-ref ref="debug" />
</logger>
<logger name="com.myapp" level="INFO">
    <appender-ref ref="main" />
</logger>
<logger name="com.myapp" level="DEBUG">
    <appender-ref ref="debug" />
</logger>

所以总结一下:

>弹簧记录器

> main – >错误
> debug – >调试

> com.myapp logger

> main – >信息
> debug – >调试

因此,我必须让日志记录器在DEBUG运行,并且一个附加器上的阈值过滤器不够细粒度。

更新增加了清晰度的问题

创建一个ThresholdLoggerFilter类,可以放在appender上,如:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <filter class="com.myapp.ThresholdLoggerFilter">
        <logger>org.springframework</logger>
        <level>ERROR</level>
    </filter>
    </appender>

以下代码工作

package com.myapp;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

public class ThresholdLoggerFilter extends Filter<ILoggingEvent> {
    private Level level;
    private String logger;

    @Override
    public FilterReply decide(ILoggingEvent event) {
        if (!isStarted()) {
            return FilterReply.NEUTRAL;
        }

        if (!event.getLoggerName().startsWith(logger))
            return FilterReply.NEUTRAL;

        if (event.getLevel().isGreaterOrEqual(level)) {
            return FilterReply.NEUTRAL;
        } else {
            return FilterReply.DENY;
        }
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    public void setLogger(String logger) {
        this.logger = logger;
    }

    public void start() {
        if (this.level != null && this.logger != null) {
            super.start();
        }
    }
}
http://stackoverflow.com/questions/10734025/logback-two-appenders-multiple-loggers-different-levels

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – logback:两个appender,多个logger,不同的级别