Log4j logging twice

I use log4j to log errors and other system information. but from information registered twice at the 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 a program starts or a failure is logged twice, anyone can help me find the reason for this.

+61
logging log4j
Apr 18 2018-11-11T00:
source share
7 answers

It seems that your messages are registered once by the root registrar and again by the specific registrar, since both applications can be configured for you (maybe in different places - in the properties file, and then in the code).

This can be resolved by setting additivity to false in your log. The Log4j manual mentions additivity in the Agents and Layouts section. Check that

+85
Apr 18 2018-11-11T00:
source share

Agree with atlantis.

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

The above property settings will cause a double log.

However adding

 log4j.additivity.org.hibernate=false 

fixed problem.

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

+33
Jun 08 '11 at 15:00
source share

For this, the XML format is used:

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

Note. By default, the flag flag is set to true for registrars.

+30
Dec 12 '11 at 8:29
source share

Just add

 logger.setadditivity(false); 

to your code ( Link ).

We have double results in the console because appenders are not single, they are additive. Value, the category inherits all applications from its ancestors (by default). If we add appender to a category and write to the same base stream (console, same file, etc.) as another application, the same log message will appear twice (or more) in the log. In addition, if two categories in the hierarchy are configured to use the same application name, Log4j will write twice for this application. Configured for this category

+3
Dec 29 '15 at 8:53
source share

If you can run the program using the Java debugger, place a breakpoint in the program where one of these double registration calls occurs.

Examine the logger object in the debugger. If it is org.apache.log4j.Logger (v 1.2.x), then it may have AppenderAttachableImpl. You can request AppenderAttachableImpl for a list of applications.

If you find more than one application, this can be a problem - and the key to fixing it.

+2
Sep 23 '14 at 3:21
source share

A potential alternative to setting the additivity property is to check your registrars from the most specific to the most general. In the following example, we expect to see dual logging in the Console for any log events that occur in foo.bar.LoggingExampleClass. It would be safe to remove the additional console appender from foo.bar.LoggingExampleClass Logger, as it is already covered by the root logger.

 <Logger name="foo.bar.LoggingExampleClass" level="DEBUG"> <AppenderRef ref="Console" /> <!-- THIS APPENDER COULD BE REMOVED --> <AppenderRef ref="FooBarPackageLogging" /> </Logger> <Root level="WARN"> <AppenderRef ref="Console" /> <AppenderRef ref="MainLogFile" /> </Root> 

There are trade-offs with both the additivity adjustment approach and the application adjustment approach. Disabling additivity may inadvertently stop the use of the desired added level logger. In the above example, setting the additivity="false" property in the foo.bar.LoggingExampleClass Logger file means that the logging event will not be added to the MainLogFile specified in the root log.

On the other hand, relying on parent applications can be problematic if parent consoles are changed without exploring the effects for more granular registrars. For example, suppose that there is a requirement that foo.bar.LoggingExampleClass events are written to the Console. They are currently in the above configuration configuration because of additivity, even if the application foo.bar.LoggingExampleClass Logger Console is uninstalled. However, if the console application was also removed from the root registrar without any additional settings, this requirement will no longer be met.

0
Sep 29 '17 at 18:38
source share

I had the same problem and fixed it by removing all applications from the root registrar. I don’t know why, but solve my problem, and I share:

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

Without this extra line, even when additivity is set to false, whenever I register with my csvLogger or txtLogger, it is registered twice.

0
Jan 31 '19 at 10:12
source share



All Articles