3
votes

I am having following xml config for log4j with 3 appenders. And want to configure logger the following way:

  1. All messages more than INFO are written to STDOUT and global log FILE appenders

  2. But for some class Oauth I want to have extra log file OAUTHFILE

  3. Some classes are filtered and write messages to LOG only on error level this can be achieved by

    <logger name="application" level="ERROR" />
    
  4. some classes are filtered and write messages to its own log file. This is also solved with additivity equal false flag.

    <logger name="MapActor" level="DEBUG" additivity="false">
         <appender-ref ref="MAPACTORFILE" />
    </logger>
    

But how can I solve the 2nd problem. The folowing configuration produces debug and error messages in 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>
1
I think that the configuration is OK... I think so. By the way, see this example.Paul Vargas

1 Answers

0
votes

Actually I have found the solution for second task. I should make 2 STDOUT and FILE appenders - one for general logging and the second one 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 as extra appender-ref for classes that should produce error log to general stdout like here:

<logger name="MapActor" level="DEBUG" additivity="false">
    <appender-ref ref="MAPACTORFILEAPPENDER" />
    <appender-ref ref="STDOUTERR" />
</logger>