Master Logback Filters: Control Log Output with LevelFilter & ThresholdFilter
This article explains how to use Logback's built‑in LevelFilter and ThresholdFilter to restrict log output by level, demonstrates XML configurations for error‑only and warn‑and‑above logging, and shows how to implement a custom filter for complex scenarios.
When you need to control the range of log output in Logback, you can configure different ch.qos.logback.classic.filter.LevelFilter and ch.qos.logback.classic.filter.ThresholdFilter for each appender.
LevelFilter example (ERROR only)
<appender name="ERROR_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/error.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern>
</encoder>
</appender>The LevelFilter compares the log event's level and accepts only the specified level (here ERROR), denying all others.
ThresholdFilter example (WARN and above)
<appender name="WARN_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/warn_error.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern>
</encoder>
</appender>The ThresholdFilter allows all events whose level is at least the specified threshold, so INFO, DEBUG, and TRACE are filtered out.
Custom filter for complex rules
If built‑in filters are insufficient, you can implement your own by extending ch.qos.logback.core.filter.Filter<ILoggingEvent> and overriding the decide method.
public class MyFilter extends Filter<ILoggingEvent> {
@Override
public FilterReply decide(ILoggingEvent event) {
if (event.getLevel() == Level.ERROR) {
switch (event.getLoggerName()) {
case "org.springframework.cloud.sleuth.instrument.web.ExceptionLoggingFilter":
return FilterReply.DENY;
}
}
return FilterReply.ACCEPT;
}
}After implementing the filter, reference it in the appender configuration:
<appender name="WARN_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/warn_error.log</file>
<filter class="com.didispace.log.filter.ExceptionClassFilter"/>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern>
</encoder>
</appender>For more details, refer to the official Logback filters documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
