Registration levels of individual packages for different backup applications

I have this simple log configuration file containing two applications and some custom logging levels based on the package name.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <charset>UTF-8</charset> <pattern>%date{HH:mm}\t%-5level\t%msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>my_logger.log</file> <encoder> <charset>UTF-8</charset> <pattern>%date{dd MMM HH:mm}|%class{0}|%-5level|%msg%n</pattern> </encoder> </appender> <!-- custom logging levels --> <logger name="myapp.package1" level="INFO" /> <logger name="myapp.package2" level="INFO" /> <root> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </configuration> 

In this configuration, custom logging levels apply to both applications.

How can I change it so that only ConsoleAppender uses these custom logging levels and FileAppender adheres to standard levels?

+5
source share
1 answer

If you do not want to implement a custom filter, you can create a new appender with a fixed threshold (in your case INFO ):

 <appender name="INFO_CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> ... <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> </appender> 

Then for your custom logging levels add INFO_CONSOLE and your FILE appender. The additivity="false" attribute prevents the log from being logged into the CONSOLE application inherited from root .

 <logger name="myapp.package1" additivity="false"> <appender-ref ref="INFO_CONSOLE" /> <appender-ref ref="FILE" /> </logger> 

This should be log DEBUG and above for FILE and CONSOLE appenders, with the exception of myapp.package1 , which will only write INFO and above to CONSOLE .

+4
source

Source: https://habr.com/ru/post/1245321/


All Articles