Dynamically Change Log Levels with Logback TurboFilter in Java
This article explains why and how to use Logback's TurboFilter to dynamically modify log levels, compares it with traditional filters, provides a concrete Java implementation that redirects ERROR logs to WARN, and shows the necessary XML and programmatic configurations.
When multiple Java frameworks are stacked, lower‑level frameworks may log errors that cause duplicate alerts during exception propagation. Modifying source code is impractical, so Logback's TurboFilter offers a way to dynamically control and rewrite such log statements.
Unlike filters implemented via ch.qos.logback.core.filter.Filter that are bound to an Appender, a TurboFilter is attached to the logging context and can inspect every logging request, providing rich information for decision making.
The following example creates a custom TurboFilter that intercepts ERROR logs from
org.springframework.cloud.sleuth.instrument.web.ExceptionLoggingFilter, denies the original ERROR, and re‑logs the same message at WARN level.
public class ForceWarnFilter extends TurboFilter {
@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable throwable) {
if (level == Level.ERROR && logger.getName().equals("org.springframework.cloud.sleuth.instrument.web.ExceptionLoggingFilter")) {
logger.warn(marker, format, params);
return FilterReply.DENY;
}
return FilterReply.NEUTRAL;
}
}To activate this filter, add it to logback.xml:
<configuration>
<turboFilter class="com.didispace.log.filter.ForceWarnFilter" />
...
</configuration>Alternatively, you can register the filter programmatically in the application’s main class:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.addTurboFilter(new ForceWarnFilter());For more details on Logback filters, refer to the official documentation at logback.qos.ch/manual/filters.html .
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.
