本文介绍了logback-重新映射特定记录器的日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个logback配置,该配置具有一个带阈值过滤器的附加器:

I have a logback configuration that has an appender with a threshold filter:

<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>INFO</level>
  </filter>
  ...
</appender>

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

This ensures that only info and higher (warn, error) get logged to syslog. However, one of the 3rd party libraries we use is logging a particular event at DEBUG, and I would like to log this event to syslog. The first approach I had in mind was to try remap the log level in the logger, but am not sure if this is possible? Something like:

<logger name="akka.some.Thing" level="DEBUG" logAs="INFO">
  <appender-ref ref="SYSLOG" />
</logger>

显然,"logAs"参数不存在,所以我不能这样做.登录akka.some.thing到SYSLOG附加程序,同时将过滤器留给其他记录器的最佳方法是什么?

obviously, the "logAs" parameter doesn't exist, so I can't do that. What would be the best approach to logging akka.some.Thing to the SYSLOG appender while leaving the filter in place for other loggers?

另一种方法是创建一个名为SYSLOG2的第二个附加器,该附加器没有适当的过滤器,并将特定的记录器设置为使用该附加器,但是想知道是否有一种方法可以仅配置一个SYSLOG附加器. ..

The other approach would be to create a 2nd appender called SYSLOG2 that doesn't have the filter in place and set the specific logger to use that, but was wondering if there was a way to configure logback with just 1 SYSLOG appender...

谢谢

推荐答案

我知道这是一个老问题-但实际上可以用单个SyslogAppender来完成OP想要做的事情.

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

如果其他人正在寻找有关如何重新映射的示例,则可以查看 org.springframework.boot.logging.logback.LevelRemappingAppender 类.使用该附加程序,既可以重新映射最终用于日志事件的附加程序,又可以重新映射用于最终日志事件的级别-例如通过将DEBUG级别更改为INFO级别.

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.

logback配置文件中的用法示例(取自 https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/org/springframework /boot/logging/logback/defaults.xml ):

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>

请注意,重新映射到特定的目标记录器可能会使查找原始日志事件的源代码变得更加困难-因此请谨慎使用.

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.

这篇关于logback-重新映射特定记录器的日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:51