1

我正在尝试配置 logback-classic 来处理以下情况:所有来自 my.class 且具有 debug 或更高级别的消息都应发送到 stdout appender。

并且所有警告级别或更高级别的错误消息应该发送到 altout 附加程序,无论它们来自何处。但我似乎无法让它工作。

我已经尝试过这个配置,但问题是如果我将 additivity 设置为 true,那么所有记录到 stdout 的消息也会被发送到 altout,无论它们的警告级别如何。但是,如果我设置 additivity=false 则不会将来自 my.server 的消息发送到 altout,即使它们具有警告/错误级别。

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>Stdout: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="ALTOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>Altout: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name='my.class'  level='debug' additivity="false">
    <appender-ref ref="ALTOUT" />
  </logger>

  <root level="warn">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
4

2 回答 2

2

Your configuration seems to be correct, except that you inverted STDOUT and ALTOUT (as per your initial description). Setting additivity="true" will log messages to both STDOUT and ALTOUT using the specified level.

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>Stdout: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="ALTOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>Altout: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="Flop"  level="error" additivity="true">
    <appender-ref ref="STDOUT" />
  </logger>

  <root level="warn">
    <appender-ref ref="ALTOUT" />
  </root>
</configuration>

With the following Java classes:

public class Flop {

    private Logger log = LoggerFactory.getLogger(Flop.class);


    public void plop() {
        log.debug("Flop debug");
        log.warn("Flop warn");
        log.error("Flop error");
    }
}

public class Main {

    private Logger log = LoggerFactory.getLogger(Main.class);

    public void prout() {
        log.debug("Main debug");
        log.warn("Main warn");
        log.error("Main error");
    }
    public static void main(String[] args) {
        new Main().prout();
        new Flop().plop();
    }
}

The result will be:

Altout: 14:55:26.288 [main] WARN  com.excilys.Main - Main warn
Altout: 14:55:26.292 [main] ERROR com.excilys.Main - Main error
Stdout: 14:55:26.292 [main] ERROR com.excilys.Flop - Flop error
Altout: 14:55:26.292 [main] ERROR com.excilys.Flop - Flop error

I.e. Main logs warn and above on STDOUT and Flop logs error and above on both STDOUT/ALTOUT. Setting additivity="false" will cause Flop to log only in STDOUT (but that's not what you want, as I understand from your question).

于 2012-06-27T12:57:45.363 回答
2

使用 ALTOUT 上的http://logback.qos.ch/manual/filters.html#thresholdFilter过滤所有低于警告的事件。

于 2012-06-27T08:53:44.777 回答