You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@logging.apache.org by "little-hue (via GitHub)" <gi...@apache.org> on 2025/09/05 23:18:08 UTC

[D] How to change log level dynamically per request level? [logging-log4j2]

GitHub user little-hue created a discussion: How to change log level dynamically per request level?

I want to change log level per request dynamically? If the root level is set to INFO, how do I change the log level to DEBUG dynamically while the application is running. 

I tried ThreadContext log level filter, the logger won't change to DEBUG if my root level is set to be INFO in the log4j2.xml. 


For example, below is the log4j2.xml in my helidon application.

`<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30" shutdownHook="disable">
    <Appenders>
        <Console name="JSONLogConsoleAppender" target="SYSTEM_OUT">
            <SpectraJsonLayout compact="false" eventEol="true"/>
        </Console>
        <Async name="AsyncCAppender" bufferSize="256">
            <AppenderRef ref="JSONLogConsoleAppender"/>
        </Async>
    </Appenders>
    <Loggers>
        <Root level="${sys:spectra-log-level:-INFO}">
            <AppenderRef ref="AsyncCAppender"/>
        </Root>
    </Loggers>
</Configuration>`


Any suggestion on how to achieve log level change per request only?

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

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How to change log level dynamically per request level? [logging-log4j2]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
GitHub user ppkarwasz added a comment to the discussion: How to change log level dynamically per request level?

Hi @little-hue,

A `DynamicThresholdFilter` can be used to override the level check on the root logger, but there are a couple of important caveats:

* The filter must be defined as a **global filter** (a direct child of `<Configuration>`), not attached to an appender, appender reference or logger.
* The filter must return `ACCEPT` for the event to pass through.

Log4j Core evaluates filters in a strict order. The initial level check on the logger happens very early in the process, so filters attached lower in the chain (e.g., on the root logger or an appender) cannot influence that decision. That’s why placing the `DynamicThresholdFilter` at the global level is essential.

You can find more detail in the [Log4j 2 filter documentation](https://logging.apache.org/log4j/2.x/manual/filters.html), which explains the evaluation order and scope of filters in different positions on the logging pipeling


GitHub link: https://github.com/apache/logging-log4j2/discussions/3917#discussioncomment-14347631

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How to change log level dynamically per request level? [logging-log4j2]

Posted by "little-hue (via GitHub)" <gi...@apache.org>.
GitHub user little-hue edited a discussion: How to change log level dynamically per request level?

I want to change log level per request dynamically? If the root level is set to INFO, how do I change the log level to DEBUG dynamically while the application is running. 

I tried ThreadContext log level filter, the logger won't change to DEBUG if my root level is set to be INFO in the log4j2.xml. 


For example, below is the log4j2.xml in my helidon application.
```

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30" shutdownHook="disable">
    <Appenders>
        <Console name="JSONLogConsoleAppender" target="SYSTEM_OUT">
            <SpectraJsonLayout compact="false" eventEol="true"/>
        </Console>
        <Async name="AsyncCAppender" bufferSize="256">
            <AppenderRef ref="JSONLogConsoleAppender"/>
        </Async>
    </Appenders>
    <Loggers>
        <Root level="${sys:spectra-log-level:-INFO}">
            <AppenderRef ref="AsyncCAppender"/>
        </Root>
    </Loggers>
</Configuration>

```

Any suggestion on how to achieve log level change per request only? I am using helidon framework 

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

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How to change log level dynamically per request level? [logging-log4j2]

Posted by "little-hue (via GitHub)" <gi...@apache.org>.
GitHub user little-hue edited a discussion: How to change log level dynamically per request level?

I want to change log level per request dynamically? If the root level is set to INFO, how do I change the log level to DEBUG dynamically while the application is running. 

I tried ThreadContext log level filter, the logger won't change to DEBUG if my root level is set to be INFO in the log4j2.xml. 


For example, below is the log4j2.xml in my helidon application.
```

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30" shutdownHook="disable">
    <Appenders>
        <Console name="JSONLogConsoleAppender" target="SYSTEM_OUT">
            <SpectraJsonLayout compact="false" eventEol="true"/>
        </Console>
        <Async name="AsyncCAppender" bufferSize="256">
            <AppenderRef ref="JSONLogConsoleAppender"/>
        </Async>
    </Appenders>
    <Loggers>
        <Root level="${sys:spectra-log-level:-INFO}">
            <AppenderRef ref="AsyncCAppender"/>
        </Root>
    </Loggers>
</Configuration>

```

Any suggestion on how to achieve log level change per request only?

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

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org