'how to log only one level with log4j2?
I'm using log4j2 in my application.
What I want is everything up to 'debug' to go to console, everything up to 'info' to go to myapp.log, and ONLY 'info' to go to 'myapp-audit.log'.
The reason is, INFO mostly consists of successful modifications to data (ex. 'user created', 'user updated', 'user deleted', and so on). If is effectively an audit log of data modifications.
But I can't get figure out how to do it.
How do I get ONLY 'info' to get logged to 'myapp-audit.log'? Here's my current configuration ...
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="LogFile" fileName="myapp.log">
<PatternLayout
pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="AuditFile" fileName="myapp-audit.log">
<PatternLayout
pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console" level="debug" />
<appender-ref ref="LogFile" level="info" />
<appender-ref ref="AuditFile" level="info" /> <!-- I want ONLY 'info' here -->
</root>
</loggers>
</configuration>
Solution 1:[1]
If you specify INFO in the appender-ref, the appender will receive INFO, WARN, ERROR and FATAL events. You can further restrict to only INFO by filtering out WARN, ERROR and FATAL level events:
<File name="AuditFile" fileName="myapp-audit.log">
<PatternLayout
pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%nZ" />
<Filters>
<!-- First deny warn, error and fatal messages -->
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<!-- Then accept info, warn, error, fatal and deny debug/trace -->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</File>
Solution 2:[2]
You can't do that directly since the info
level includes everything "above" - warn, error, fatal.
What you can do is create a separate audit logger.
in the class:
Logger log = LogManager.getLogger("audit");
in the xml:
<Logger name="audit" level="info">
<Appender-ref ref="AuditFile" level="info" />
</Logger>
Or you can use the RoutingAppender (you can use something other than ThreadContext
):
ThreadContext.put("ROUTINGFLAG", "audit");
log.info("...");
ThreadContext.remove("ROUTINGFLAG");
in the xml:
...
<Routing name="Routing">
<Routes pattern="$${ctx:ROUTINGFLAG}">
<Route AppenderRef="LogFile"/>
<Route AppenderRef="AuditFile" key="audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="debug">
<Appender-ref ref="Console" level="debug"/>
<AppenderRef ref="Routing" level="info"/>
</Root>
</Loggers>
Solution 3:[3]
You can use LevelRangeFilter. It has minLevel and maxLevel properties(and onMatch and onMismatch, ofcourse, too).
For example(in json) we need to print log in console only on info and warn levels:
"Appenders": {
"Console": {
"PatternLayout": {
"pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
},
"name": "Console",
"target": "SYSTEM_OUT",
"LevelRangeFilter": {
"minLevel": "warn",
"maxLevel": "info",
"onMatch": "ACCEPT",
"onMismatch": "DENY"
}
}
}
And if you want only info level then write "info" to both properties min and max.
Solution 4:[4]
Complementing Remko Popma's answer - you can deny anything above and below desired level using two Threshold Filters.
<File name="AuditFile" fileName="myapp-audit.log">
<PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%nZ" />
<Filters>
<!-- Deny anything above INFO level -->
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
<!-- Deny anything below INFO level -->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</File>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Remko Popma |
Solution 2 | evanwong |
Solution 3 | Ihorko.bk |
Solution 4 | Artur ?ysik |