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.

Programmer DD
Programmer DD
Programmer DD
Dynamically Change Log Levels with Logback TurboFilter in Java

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 .

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaloggingDynamicLogLevelTurboFilter
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.