LOG4J2: Multiple loggers, to multiple appenders, at different logging levels

北慕城南 提交于 2021-02-09 11:30:49

问题


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:


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.



来源:https://stackoverflow.com/questions/38778831/log4j2-multiple-loggers-to-multiple-appenders-at-different-logging-levels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!