Log4j xml configuration, write multiple Logger to file and console
I have xml config for log4j with 3 appendices. And you want to configure the recorder as follows:
-
All messages other than INFO are written to STDOUT and global applications FILE FILE
-
But for some Oauth class I want to have an additional OAUTHFILE log file
-
Some classes are filtered and only log messages at the error level, this can be achieved with
<logger name="application" level="ERROR" />
-
some classes are filtered and write messages to their own log file. This is also handled with a false additivity flag.
<logger name="MapActor" level="DEBUG" additivity="false"> <appender-ref ref="MAPACTORFILE" /> </logger>
But how can I solve the second problem. The following configuration generates debug and error messages on STDOUT and! in OAUTHFILE
<configuration>
...
<appender name="STDOUT" ...></appender>
<appender name="FILE" ...>
</appender>
<appender name="OAUTHFILE" ...>
</appender>
...
<logger name="controllers.OAuth" level="DEBUG" additivity="false">
<appender-ref ref="OAUTHFILE" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
source to share
I actually found a solution for the second problem. I have to make 2 STDOUT and FILE applications - one for general logging and the other with filtering
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
<appender name="STDOUTERR" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
And use the second one as an additional appender-ref for classes that should generate an error log for the common stdout, like here:
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILEAPPENDER" />
<appender-ref ref="STDOUTERR" />
</logger>
source to share