I have this simple Logback config file, containing two appenders and some custom logging levels based on 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>
With this configuration, the custom logging levels are applied for both appenders.
How can I change it so that only ConsoleAppender
uses these custom logging levels, while FileAppender
sticks to default levels?
You may specify the location of the default configuration file with a system property named "logback. configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
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.
7. Appenders Loggers pass LoggingEvents to Appenders. Appenders do the actual work of logging. We usually think of logging as something that goes to a file or the console, but Logback is capable of much more. Logback-core provides several useful appenders. 7.1. ConsoleAppender We've seen ConsoleAppender in action already.
A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations. A Logger can have more than one Appender.
As such, logback configuration files do not provide any syntactical means for sharing encoders or layouts. By default, appenders are cumulative: a logger will log to the appenders attached to itself (if any) as well as all the appenders attached to its ancestors.
ExampleAppender's Logger is a child of Example's Logger. All loggers are descendants of the predefined root logger. A Logger has a Level, which can be set either via configuration or with Logger.setLevel (). Setting the level in code overrides configuration files.
If you don't want to implement a custom filter, you can create a new appender with a fixed threshold (in you 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 the INFO_CONSOLE
and your FILE
appender. The additivity="false"
attribute prevents the logger from logging to the CONSOLE
appender inherited from root
.
<logger name="myapp.package1" additivity="false">
<appender-ref ref="INFO_CONSOLE" />
<appender-ref ref="FILE" />
</logger>
This should log DEBUG
and above to the FILE
and CONSOLE
appenders, except for myapp.package1
which will only log INFO
and above to CONSOLE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With