logback - 重新映射特定记录器的日志级别

Bre*_*ett 8 logback

我有一个logback配置,其中包含一个带有阈值过滤器的appender:

<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>INFO</level>
  </filter>
  ...
</appender>
Run Code Online (Sandbox Code Playgroud)

这可确保只将info和更高(警告,错误)记录到syslog中.但是,我们使用的第三方库之一是在DEBUG中记录特定事件,我想将此事件记录到syslog中.我想到的第一种方法是尝试重新映射记录器中的日志级别,但我不确定这是否可行?就像是:

<logger name="akka.some.Thing" level="DEBUG" logAs="INFO">
  <appender-ref ref="SYSLOG" />
</logger>
Run Code Online (Sandbox Code Playgroud)

显然,"logAs"参数不存在,所以我不能这样做.将akka.some.Thing记录到SYSLOG appender同时为其他记录器留下过滤器的最佳方法是什么?

另一种方法是创建一个名为SYSLOG2的第二个appender,它没有适当的过滤器并设置特定的记录器来使用它,但是想知道是否有一种方法只用1个SYSLOG appender来配置logback ...

谢谢,

joe*_*son 7

I know this is an old question - but it is actually possible to do what the OP wants to do with a single SyslogAppender.

If others are searching for an example of how to remap you can take a look at the org.springframework.boot.logging.logback.LevelRemappingAppender class. With that appender it is possible to both remap what appender is finally used for the log event, and it is also possible to remap the level that is used for the final log event - e.g. by changing a DEBUG level into an INFO level.

Usage example in logback config file (taken from https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml):

<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
    <!-- Optional: specify the destination logger the event ends up in -->
    <destinationLogger>org.springframework.boot</destinationLogger>
    <!-- Optional: specify log level remapping  -->
    <remapLevels>INFO->DEBUG,ERROR->WARN</remapLevels>
</appender>

<logger name="org.thymeleaf" additivity="false">
    <appender-ref ref="DEBUG_LEVEL_REMAPPER"/>
</logger>
Run Code Online (Sandbox Code Playgroud)

Note that remapping to a specific destination logger can make it harder to find the source code of the original log event - so use it with care.