Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j logging twice

Tags:

logging

log4j

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.

like image 504
Talha Bin Shakir Avatar asked Apr 18 '11 06:04

Talha Bin Shakir


People also ask

Why is Log4j logging twice?

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).

Why logs are printing twice?

It's called additivity. Disabling additivity to prevent duplicate logging: In some cases, logging output is executed twice to a log destination. This is a side effect of the Log4j additivity which is active for each logger by default.

What is additivity in Log4j?

Additivity is set to true by default, that is children inherit the appenders of their ancestors by default. If this variable is set to false then the appenders found in the ancestors of this logger are not used.


Video Answer


2 Answers

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

like image 175
atlantis Avatar answered Oct 20 '22 03:10

atlantis


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

like image 45
Gervase Avatar answered Oct 20 '22 05:10

Gervase