Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What kinds of filters exist in Logback and Log4j2 (level filters, threshold filters, evaluator or script filters, turbo filters, MDC or marker filters), and give one practical use for a filter versus simply setting a logger level.

A logger level makes one decision for a logger and everything below it, while a filter can make finer decisions at other points: per appender, before the event is even created, or based on markers and MDC values. Logback has LevelFilter, ThresholdFilter and EvaluatorFilter on appenders plus TurboFilters such as MDCFilter and DuplicateMessageFilter; Log4j2 has ThresholdFilter, MarkerFilter, BurstFilter, DynamicThresholdFilter and others that can sit on the context, a logger, an appender or an appender reference. A typical use is sending only ERROR to an alert appender, or turning on DEBUG for one user carried in the MDC without changing any level.

Why this question is asked

Most of us set logger levels and never touch filters, and that works until we need two different behaviours from the same event: everything in the file, only errors in the alert channel; INFO for everyone, DEBUG for the one customer with a problem. Interviewers ask about filters to see whether we understand where in the logging pipeline each decision happens, because that decides both what a filter can do and what it costs. In this answer, we will recap the path an event takes, look at Logback's filters and its turbo filters, then at Log4j2's filters and where they can be attached, explain the ACCEPT, DENY and NEUTRAL results, and finish with two practical uses.

1A Quick Recap: The Path of a Log Event

When we call log.debug(...), the framework first decides whether the event should exist at all. A logger level, set on a logger such as com.shop, is the main tool here: if com.shop is at INFO, the debug call returns almost immediately and no event object is created. If the event passes, it travels to every appender attached to the logger and its parents. An appender is the component that writes the event somewhere, such as the console, a file or a network endpoint.

A filter is a small piece of code that looks at an event and answers ACCEPT, DENY or NEUTRAL. Where it sits on this path decides what it can do. A filter on an appender can only shape what that one appender writes. A filter that runs before the level check can let an event through that the level would have stopped, or stop one the level would have allowed.

Step throughWhere Logback filters run
  1. log.debug(...) is called
  2. TurboFiltersrun before the event exists; may ACCEPT, DENY or stay NEUTRAL
  3. Logger level checkskipped when a TurboFilter said ACCEPT
  4. Event created and sent to appenders
  5. Appender filtersLevelFilter, ThresholdFilter, EvaluatorFilter per appender
  6. Appender writes the event

6 steps in order. Play them, or step through them one at a time.

2Logback's Appender Filters

Logback attaches regular filters to appenders. ThresholdFilter denies events below a level and is neutral for the rest. LevelFilter matches one exact level and lets us choose what happens on a match and on a mismatch, which is how we build an appender that takes only WARN, nothing above and nothing below. EvaluatorFilter delegates to an evaluator, such as OnMarkerEvaluator or OnErrorEvaluator; older versions also offered a Janino-based Java expression evaluator, which recent 1.5 releases deprecated or removed for security reasons, so check your version before relying on it.

XML
<appender name="ALERTS" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level> <!-- DEBUG, INFO, WARN denied; ERROR neutral, so written -->
</filter>
<encoder><pattern>%d %-5level %logger - %msg%n</pattern></encoder>
</appender>
<appender name="WARN_ONLY" class="ch.qos.logback.core.FileAppender">
<file>warn.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch> <!-- WARN written -->
<onMismatch>DENY</onMismatch> <!-- every other level dropped -->
</filter>
<encoder><pattern>%d %msg%n</pattern></encoder>
</appender>

3Logback's TurboFilters

TurboFilters are different. They are attached to the whole logger context and run before the event object is created, so they are cheap and they run before the level check. That position lets them decide based on the logger, the level, a marker or the MDC. MDCFilter matches a value of an MDC key, MarkerFilter matches a marker, DuplicateMessageFilter drops repeats of the same message template after a set number, and DynamicThresholdFilter changes the effective threshold per MDC value.

Because they run first, a TurboFilter that returns ACCEPT makes Logback skip the logger level check for that call. That is what makes per-user DEBUG possible with the global level left at INFO.

XML
<!-- DEBUG for user u-829 only; everyone else keeps the normal levels -->
<turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
<MDCKey>userId</MDCKey>
<Value>u-829</Value>
<OnMatch>ACCEPT</OnMatch> <!-- level check skipped for this user -->
<OnMismatch>NEUTRAL</OnMismatch> <!-- normal level check for everyone else -->
</turboFilter>
<logger name="com.shop" level="INFO"/>
// MDC userId=u-829: log.debug("cart {}", cart) is written
// MDC userId=u-100: log.debug("cart {}", cart) is dropped by the INFO level

4Log4j2's Filters and Where They Attach

Log4j2 has a similar set with different names. ThresholdFilter compares the level, MarkerFilter matches a marker, BurstFilter limits how many events at or below a level pass per second, DynamicThresholdFilter lowers or raises the threshold based on a ThreadContext value (ThreadContext is Log4j2's name for the MDC), and there are filters for regular expressions, scripts, time windows and more.

The key difference is placement. A Log4j2 filter can sit in four places: on the whole configuration (context-wide, evaluated before the logger level, like Logback's turbo filters), on a logger, on an appender, or on an appender reference inside a logger. The appender reference is handy: the same file appender can take everything from one logger but only ERROR from another.

Common filters side by side
NeedLogbackLog4j2
Drop below a levelThresholdFilter (appender)ThresholdFilter
Exactly one levelLevelFilterLevelRangeFilter
Match a markerMarkerFilter (turbo) or OnMarkerEvaluatorMarkerFilter
Level per MDC valueDynamicThresholdFilter, MDCFilter (turbo)DynamicThresholdFilter
Limit repeats or rateDuplicateMessageFilter (turbo)BurstFilter
Check exact class names against your version.

5Filter or Logger Level: When Each Fits

If the question is 'how much does this package log', a logger level is the right tool: it is the fastest check and the easiest to read. A filter earns its place when one level cannot express the rule. Sending only ERROR events to an alert channel while the file keeps everything is the classic case, because the logger level must stay at INFO for the file and only the alert appender should be stricter. The second classic case is the MDC-based DEBUG we saw above: support can watch one user's requests in detail while 2,000 other requests per second log at INFO as usual.

So a simple rule of thumb is that levels decide what a logger produces, and filters decide where it goes or make exceptions for specific contexts.

6Follow-up questions

What do filter results ACCEPT, DENY and NEUTRAL mean in the chain?

Filters form a chain and each one answers one of three values. DENY drops the event immediately and no later filter runs. ACCEPT takes the event immediately, skipping the remaining filters; for a turbo filter or a Log4j2 context-wide filter it also skips the logger level check. NEUTRAL means 'I have no opinion', so the next filter decides, and if every filter is neutral the normal behaviour applies: the level check for turbo filters, and writing the event for appender filters.

Why are TurboFilters cheaper than appender filters?

They run before Logback creates the event object, formats anything or walks the appender list, so denying an event there costs about as much as a level check. An appender filter only runs after the event exists and has been dispatched, so dropping it there means the earlier work was wasted.

Can a filter on an appender let DEBUG through if the logger is at INFO?

No. By the time an event reaches an appender, the logger level has already rejected the DEBUG call, so the appender never sees it. To make an exception to the level you need a filter that runs before the level check, a Logback TurboFilter or a Log4j2 context-wide filter.

7Conclusion

In this answer, we saw that a logger level makes one fast decision per logger, while filters make decisions at other points in the pipeline. Logback offers LevelFilter, ThresholdFilter and EvaluatorFilter on appenders and TurboFilters such as MDCFilter, DynamicThresholdFilter and DuplicateMessageFilter before the event exists; Log4j2 offers ThresholdFilter, MarkerFilter, BurstFilter, DynamicThresholdFilter and others on the context, loggers, appenders or appender references. Results are ACCEPT, DENY or NEUTRAL, and the practical uses are routing only errors to alerts and enabling DEBUG for one user through the MDC.

What to say in the interviewAbout 80 seconds aloud

Open with

A logger level decides at the logger, for that logger and its children, and it's the cheapest check there is. Filters work at other points. In Logback, regular filters sit on appenders: ThresholdFilter drops events below a level, LevelFilter matches one exact level with onMatch and onMismatch, and EvaluatorFilter uses an evaluator such as a marker evaluator.

Then, if they want more

Then there are TurboFilters, which are context-wide and run before the event is even created and before the level check, so they can decide based on markers or MDC values, for example MDCFilter, DynamicThresholdFilter or DuplicateMessageFilter.

Log4j2 has ThresholdFilter, MarkerFilter, BurstFilter for rate limiting, DynamicThresholdFilter keyed on the ThreadContext, and others, and they can be attached to the whole configuration, a logger, an appender or an appender reference. Each filter returns ACCEPT, DENY or NEUTRAL: deny drops it, accept takes it and skips the rest, neutral passes to the next filter.

A practical use: keep the loggers at INFO so the file gets everything, but put a ThresholdFilter at ERROR on the appender that sends alerts. Another one: put the user id in the MDC and use a turbo filter so one user's requests log at DEBUG while everyone else stays at INFO, without touching any level.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What kinds of filters exist in Logback and Log4j2 (level filters, threshold filters...": https://scrivio-interview-library.netlify.app/java/java-logging/55481c8ec2/