log4j logging twice

2020-02-08 22:18发布

I am using log4j to log error and other system information. but come of the info logged twice at INFO level.

public static void main(final String... args) throws Exception {

    LOGGER.info("program started");
    try {
        // try body codes
    } catch (Exception ex) {
        LOGGER.info("program start-up failed.",ex);
    }
}

however when the program starts or failed the information logged twice, any one can help me to find what could be the reason of that.

标签: logging log4j
8条回答
做自己的国王
2楼-- · 2020-02-08 22:31

For those use XML format:

<logger name="package.class" additivity="false">
    <level value="info" />
    <appender-ref ref="file" />
    <appender-ref ref="console" />
</logger>

Note: By default, Loggers have their additivity flag set to true.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-08 22:32

Agree with atlantis.

log4j.rootCategory=INFO, console
log4j.logger.org.hibernate=INFO

The above property settings will cause double logging.

However adding

log4j.additivity.org.hibernate=false

fixed the issue.

Check out page 62 of this book. http://books.google.com/books?id=hZBimlxiyAcC&printsec=frontcover#v=onepage&q&f=false

查看更多
何必那么认真
4楼-- · 2020-02-08 22:38

In your resources/log4.properties file.

In that configuration file, if you have "log4j.rootLogger= DEBUG, file", then don't include "log4j.logger.org.springframework=DEBUG, file". Just keep the log4j.rootLogger part.

查看更多
淡お忘
5楼-- · 2020-02-08 22:44

Just simply add

logger.setadditivity(false);

to your code (Reference).

We are having double results in the console, it's because appenders are not singletons, they are additive. Meaning, a category inherits all the appenders from its ancestors (by default). If we add an appender to a category and it writes to the same underlying stream (console, same file etc.) as some other appender, the same log message will appear twice (or more) in the log. In addition, if two categories in a hierarchy are configured to use the same appender name, Log4j will write twice to that appender. Configured for that category

查看更多
Deceive 欺骗
6楼-- · 2020-02-08 22:47

If you can run the program with a Java debugger, put a breakpoint in the program where one of these double logging calls happen.

Examine the logger object in the debugger. If it is an org.apache.log4j.Logger (v 1.2.x) then it may have an AppenderAttachableImpl. You can query the AppenderAttachableImpl for the appender list.

If you find more than 1 appender, this could be the problem - and a clue to fixing it.

查看更多
趁早两清
7楼-- · 2020-02-08 22:47

I had the same problem, and fixed by removing all appenders from the root logger. I don't know why, but solve my problem and I'm sharing:

        // Root
    rootLogger = Logger.getRootLogger();
    rootLogger.removeAllAppenders(); // Solve my problem
        // CSV
    csvLogger = rootLogger.getLogger("csvLogger");
        // Txt
    txtLogger = rootLogger.getLogger("txtLogger");

Without this extra line, even setting additivity to false, whenever I log with my csvLogger or txtLogger it logs twice.

查看更多
登录 后发表回答