Question

Can we dsable log print for some customm creating Logger?

I have two loggers:

  1. Root logger

  2. Metrics Logger (which prints into file)

<Appenders>
    <File name="file" fileName="app.log">
        <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
        </PatternLayout>
    </File>

    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>


</Appenders>

<Loggers>

    <logger name="metrics" level="info" additivity="true">
        <AppenderRef ref="file"/>
    </logger>      

    <root level="info">
        <AppenderRef ref="STDOUT"/>
    </root>

</Loggers>

I want to disable print messages in console for File logger

Was it helpful?

Solution

The key to understanding is here http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity

The root logger is a parent of the logger "metrics". The log event of "metrics" is then passed to root logger's appender ("STDOUT"). This is known as additivity. It is possible to disable it by setting the additivity attribute on the logger "metrics" to false.

<logger name="metrics" level="info" additivity="false">
        <AppenderRef ref="file"/>
</logger>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top