LOG4J2: Multiple loggers, to multiple appenders, a

2019-02-25 13:27发布

What I'm trying to do is pretty straight forward:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="Console">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>

    <File name = "hibernateFile" fileName = "${artifactId}/logs/hibernate.log"> 
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>

     <File name = "springFile" fileName = "${artifactId}/logs/spring.log"> 
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>

    <File name = "rootFile" fileName = "${artifactId}/logs/root.log"> 
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>        

  </Appenders>
  <Loggers>
    <Root level="trace">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="rootFile"/>
    </Root>

    <Logger name ="org.springframework" level = "debug"> 
        <AppenderRef ref = "Console" level = "info"/>
        <AppenderRef ref = "springFile" level = "debug"/>
    </Logger>

     <Logger name ="org.hibernate" level = "debug"> 
        <AppenderRef ref = "Console" level ="info"/>
        <AppenderRef ref = "hibernateFile" level = "debug"/>
    </Logger>

  </Loggers>
</Configuration>

I want only important messages to be displayed on my Console appender, while I want detailed messages to be routed to the file appenders.

However, I don't want to filter the appender itself - as I may want to route some debugging or trace messages there (for example when I'm in development).

The Apache FAQ suggests that I can just filter by logging level on the AppenderRef, but I've tried this, as outlined in the above xml - and it still routes the DEBUG level messages to the console.

I've also tried adding a ThresholdFilter to the AppenderRef, but that doesn't filter it either.

 <Logger name ="org.hibernate" level = "debug"> 
    <AppenderRef ref = "Console">
         <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
    </AppenderRef>
    <AppenderRef ref = "hibernateFile" level = "debug"/>
</Logger>

1条回答
等我变得足够好
2楼-- · 2019-02-25 14:01

additivity="false" should do the trick:

<Logger name="org.hibernate" level="debug" additivity="false"> 

The additivity controls whether a log event is also still forwarded to the root logger or not.

查看更多
登录 后发表回答