Repository navigation
log4j-to-slf4j: Allow opting out of lazy level checks for Logback #4494
hiroki-yamamura
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
With
log4j-to-slf4jandlog4j-api2.26.1, SLF4J 2.0.20, and Logback 1.6.6 (root=INFO, noTurboFilter),logger.isDebugEnabled()returnsfalse, whereaslogger.atDebug()returns anSLF4JLogBuilderinstead ofLogBuilder.NOOP.As a result, a single
atDebug().log("value={}", new Supplier<?>[] {value})call evaluates the Log4jSupplieronce and invokes the returned object'stoString()once, even though the log event is ultimately discarded. Switching toSimpleProviderwith the same classpath returnsLogBuilder.NOOP, and both counts remain zero. Because libraries such as Apache POI 6.0.0 have adopted the fluent logging API, this introduces noticeable overhead in applications using Logback withoutTurboFilters.Looking at
SLF4JLogger,LAZY_LEVEL_CHECKis enabled solely based on theILoggerFactoryclass name (ch.qos.logback.classic.LoggerContext). From the context in #1287 (LOG4J2-3647) and #4068, we completely understand the rationale:TurboFilters can dynamically override logger levels (ACCEPT), so eagerNOOPreturns would bypass global filtering.However, for high-throughput applications that rely on standard level-based filtering and do not register any
TurboFilters, paying the allocation andSupplierevaluation cost on every disabled fluent log call is undesirable.Would you consider adding a way to restore eager level checks in such environments? Specifically:
log4j2.slf4j.lazyLevelCheck=falseor similar) to disable the lazy level check for applications that guarantee they do not useTurboFilters. This preserves backward compatibility by default while offering a clean escape hatch viaPropertiesUtil.LoggerContextcurrently has anyTurboFilters registered (though we recognize this introduces tighter coupling to Logback internals and potential edge cases if filters are added dynamically at runtime).To reproduce the current behavior, please see the minimal setup below.
Minimal Reproduction(pom.xml, logback.xml, DisabledFluentLog.java)
pom.xml(dependencies excerpt):logback.xml:DisabledFluentLog.java:All reactions