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.
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).
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With