Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback - remapping a log level for a specific logger

Tags:

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>

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>

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?

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...

Thanks,

like image 546
Brett Avatar asked Feb 25 '13 09:02

Brett


People also ask

How do you change the log level?

To change log levels, as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

How do you change the log level in slf4j?

When using log4j, the Logger. log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level. slf4j doesn't have a generic log() method that I can find.

How do you filter Logback logs?

In logback-classic, filters can be added to Appender instances. By adding one or more filters to an appender, you can filter events by arbitrary criteria, such as the contents of the log message, the contents of the MDC, the time of day or any other part of the logging event.


2 Answers

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>

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.

like image 151
joensson Avatar answered Sep 22 '22 12:09

joensson


What you can do, is writing a second logger + appender with the same output:

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

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

This will add your specific DEBUG tasks to the same output.

like image 44
Dimitri Dewaele Avatar answered Sep 22 '22 12:09

Dimitri Dewaele