My log4j2 log file always have double output of each line. How can I stop the duplication please?
Asked Answered
B

2

26

My log4j2 log file always has double output of each line. How can I stop the duplication please?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <properties>
        <property name="name">CernerPharmacyWebApp</property>
        <property name="pattern">%d %-5p %C{2} (%F:%L) - %m%n</property>
    </properties>
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
        </Console>
        <RollingFile name="ROLLING_FILE" fileName="..\logs\${name}.log"
                     filePattern="..\logs\${name}-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/><!-- Rotated everyday -->
                    <SizeBasedTriggeringPolicy size="50 MB"/> <!-- Or every 50 MB -->
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="error">
            <!-- <AppenderRef ref="CONSOLE"/> -->
            <AppenderRef ref="ROLLING_FILE"/>
        </Root>
        <!-- Controls packages -->
        <Logger name="com.jarmel.pharmacy.controls.BarCodeOrderServlet" level="trace">
            <!-- <AppenderRef ref="CONSOLE"/> -->
            <AppenderRef ref="ROLLING_FILE"/>
        </Logger>
        <Logger name="com.jarmel.pharmacy.controls.GetCodeValues" level="error">
            <!-- <AppenderRef ref="CONSOLE"/> -->
            <AppenderRef ref="ROLLING_FILE"/>
        </Logger>
        <Logger name="com.jarmel.pharmacy.controls.GetPersonnelServlet" level="error">
            <!-- <AppenderRef ref="CONSOLE"/> -->
            <AppenderRef ref="ROLLING_FILE"/>
        </Logger>
   </Loggers>
</Configuration>

Blockquote

Bren answered 31/1, 2014 at 2:22 Comment(1)
Please provide us with more information. Add some code that logs a message and possibly a sample of the actual output you have now. With more information you're likely to get more/better help!Odetteodeum
K
52

Both your root logger and your named loggers are referring to the same appender. Each active logger will send an event to its appender, so the appender will receive the event multiple times, once for each logger. (The root logger will get all events, the named loggers will only get the events emitted by the Logger in your code with a matching name.)

This is called Additivity. See http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity

You can set additivity="false" in the named logger config.

Kile answered 1/2, 2014 at 1:26 Comment(2)
To be precise, as an attribute in the <Logger> tagNaoma
It should also be pointed out that this affects the named logger with a log level equal to or lower than the root logger, since that is the case where both loggers are sending the same event to the appender. So additivity="false" should go on the loggers that refer to the root logger's appender and have an equal/lower log level.Sondra
R
2

There is no need of specifying a separate appender for each named logger unless the destination is different than the root logger appender. You can also rewrite as below:

<Root level="error">
        <!-- <AppenderRef ref="CONSOLE"/> -->
        <AppenderRef ref="ROLLING_FILE"/>
</Root>
<!-- Controls packages -->
<Logger name="com.jarmel.pharmacy.controls.BarCodeOrderServlet" level="trace"/>
        
<Logger name="com.jarmel.pharmacy.controls.GetCodeValues" level="error"/>
           
<Logger name="com.jarmel.pharmacy.controls.GetPersonnelServlet" level="error"/>
            
Resolvable answered 22/2, 2022 at 17:28 Comment(1)
This sounds more reasonable than "hacky" additivity="false".Zingaro

© 2022 - 2024 — McMap. All rights reserved.