Log4j logging twice

log4jlogging

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.

Best Answer

Looks like your messages are being logged once by the root logger and again by the specific logger as you may have both the appenders configured (may be at different places -in a properties file and then in code).

This can be solved by setting additivity to false on your logger. Log4j manual mentions additivity in the Appenders and Layout section.Check that out

Related Topic