Log4j dynamic threshold filter with redirection to specific file. #2863
-
Hi All, Recently i analysed dynamic threshold filter feature of log4j. It is working for the configured trace level. But my problem is that i am enabling level TRACE if request has header x-trace-enabled as true and logs are going to all appenders configured under xml file. i am using springboot framework with log4j2 and below is the configuration <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">[%-5level] %d{DEFAULT} [%t] %c{1.}%notEmpty{ - %X{requestId}} - %msg %n</Property>
<Property name="log-path">/var/log/path</Property>
</Properties>
<DynamicThresholdFilter key="x-trace-enabled" onMatch="ACCEPT" onMismatch="NEUTRAL">
<KeyValuePair key="true" value="TRACE"/>
</DynamicThresholdFilter>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
</Console>
<RollingRandomAccessFile append="true" name="RollingFile" fileName="${log-path}/info.log"
filePattern="${log-path}/info-%d{yyyy-MM-dd-HH}.%i.log.zst">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="1GB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="nomax">
<Delete basePath="${log-path}">
<IfFileName glob="info-*.log.zst"/>
<IfAccumulatedFileCount exceeds="50"/>
</Delete>
<Delete basePath="${log-path}">
<IfFileName glob="access_log*.log"/>
<IfAccumulatedFileCount exceeds="2"/>
</Delete>
<Delete basePath="${log-path}">
<IfFileName glob="management_access_log*.log"/>
<IfAccumulatedFileCount exceeds="2"/>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="error-log"
fileName="${log-path}/error.log"
filePattern="${log-path}/error-%d{yyyy-MM-dd}.%i.log.zst">
<JsonLayout compact="true" eventEol="true">
</JsonLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="500MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="nomax">
<Delete basePath="${log-path}">
<IfFileName glob="error-*.log.zst"/>
<IfAccumulatedFileCount exceeds="2"/>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="WARN" additivity="false">
<appender-ref ref="RollingFile"/>
<AppenderRef ref="error-log"/>
</Root>
<Logger name="com.example.package" level="DEBUG" additivity="false">
<appender-ref ref="RollingFile"/>
<AppenderRef ref="error-log" level="WARN" />
</Logger>
</Loggers>
</Configuration> Answers i am looking for : -
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
Log4j has a highly configurable filtering pipeline. I assume that you want to:
In the case above you can use: <!-- if `x-trace-enabled` is set, ignore the configured level -->
<DynamicThresholdFilter key="x-trace-enabled" onMatch="ACCEPT" onMismatch="NEUTRAL">
<KeyValuePair key="true" value="TRACE"/>
</DynamicThresholdFilter>
<Loggers>
<Root level="WARN">
<!-- Apply the level check again, in case:
* it was skipped, since "x-trace-enabled" is set.
* or the event comes from the `com.example.package` logger,
which has a lower (INFO) threshold. -->
<AppenderRef ref="error-log" level="WARN"/>
</Root>
<Logger name="com.example.package" level="INFO">
<AppenderRef ref="RollingFile/>
</Logger>
</Loggers> |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
The
level
attribute onRoot
is ignored if the global filter returnsACCEPT
. However thelevel
attribute ofAppenderRef
is not ignored.See Filtering Process for an in-depth description on how filters are applied and in which order.