GitHub user hiroki-yamamura created a discussion: log4j-to-slf4j: Allow opting 
out of lazy level checks for Logback

With `log4j-to-slf4j` and `log4j-api` 2.26.1, SLF4J 2.0.20, and Logback 1.6.6 
(`root=INFO`, no `TurboFilter`), `logger.isDebugEnabled()` returns `false`, 
whereas `logger.atDebug()` returns an `SLF4JLogBuilder` instead of 
`LogBuilder.NOOP`.

As a result, a single `atDebug().log("value={}", new Supplier<?>[] {value})` 
call evaluates the Log4j `Supplier` once and invokes the returned object's 
`toString()` once, even though the log event is ultimately discarded. Switching 
to `SimpleProvider` with the same classpath returns `LogBuilder.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 without `TurboFilter`s.

Looking at `SLF4JLogger`, `LAZY_LEVEL_CHECK` is enabled solely based on the 
`ILoggerFactory` class name (`ch.qos.logback.classic.LoggerContext`). From the 
context in #1287 (LOG4J2-3647) and #4068, we completely understand the 
rationale: `TurboFilter`s can dynamically override logger levels (`ACCEPT`), so 
eager `NOOP` returns would bypass global filtering.

However, for high-throughput applications that rely on standard level-based 
filtering and do not register any `TurboFilter`s, paying the allocation and 
`Supplier` evaluation cost on every disabled fluent log call is undesirable.

Would you consider adding a way to restore eager level checks in such 
environments? Specifically:

1. **(Preferred) An opt-in system property** (e.g., 
`log4j2.slf4j.lazyLevelCheck=false` or similar) to disable the lazy level check 
for applications that guarantee they do not use `TurboFilter`s. This preserves 
backward compatibility by default while offering a clean escape hatch via 
`PropertiesUtil`.
2. **Automatic detection** based on whether the Logback `LoggerContext` 
currently has any `TurboFilter`s 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.

<details>
<summary>Minimal Reproduction(pom.xml, logback.xml, 
DisabledFluentLog.java)</summary>

**`pom.xml` (dependencies excerpt):**
```xml
  <dependencies>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.26.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-to-slf4j</artifactId>
      <version>2.26.1</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>2.0.20</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.6.6</version>
    </dependency>
  </dependencies>
```

**`logback.xml`:**

```XML
<configuration>
  <root level="INFO"/>
</configuration>
```

**`DisabledFluentLog.java`:**

```Java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Supplier;

public class DisabledFluentLog {
    private static final Logger logger = 
LogManager.getLogger(DisabledFluentLog.class);

    public static void main(String[] args) {
        System.out.println("isDebugEnabled=" + logger.isDebugEnabled());

        Supplier<?>[] value = new Supplier<?>[] {
            () -> new Object() {
                {
                    System.out.println("Supplier was evaluated.");
                }

                @Override
                public String toString() {
                    System.out.println("toString() was called.");
                    return "test-value";
                }
            }
        };

        logger.atDebug().log("value={}", value);
    }
}
```
</details>

GitHub link: https://github.com/apache/logging-log4j2/discussions/4494

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to