log4j xml配置,写一些Logger到文件和控制台

时间:2022-04-09 23:02:18

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

我正在使用3个appender跟踪log4j的xml配置。并希望以下列方式配置logger:

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

    所有超过INFO的消息都写入STDOUT和全局日志FILE appender

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

    但对于某些类Oauth我想要额外的日志文件OAUTHFILE

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

    一些类被过滤并仅在错误级别上将消息写入LOG,这可以通过实现

    <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

但是我怎样才能解决第二个问题。下面的配置会在STDOUT中生成调试和错误消息!在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 个解决方案

#1


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

实际上我找到了第二个任务的解决方案。我应该制作2个STDOUT和FILE appender - 一个用于通用日志记录,第二个用于过滤

<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:

并使用第二个作为额外appender-ref的类应该产生错误日志到一般stdout像这里:

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

#1


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

实际上我找到了第二个任务的解决方案。我应该制作2个STDOUT和FILE appender - 一个用于通用日志记录,第二个用于过滤

<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:

并使用第二个作为额外appender-ref的类应该产生错误日志到一般stdout像这里:

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