3

I have implemented a custom log file using log4j for my web application. But the problem is when I am printing any thing in that log file, it prints multiple times. Whether there is no loop or any kind of iteration in the code. Can any one please help me to sort out this problem.

1
  • 1
    I expect that the problem is in your logging configuration file. Can we see it?
    – Stephen C
    Commented May 24, 2012 at 14:04

4 Answers 4

10

From: http://www.jajakarta.org/log4j/jakarta-log4j-1.1.3/docs/TROUBLESHOOT.html

Duplicates in log4j output.

The reason for observing duplicates in log4j output is either due to having added the same appender multiple times to the same category (typically root) or having added the same appender to different categories ignoring the fact that appenders are inherited cumulatively.

log4j does not eliminate appender duplicates. In other words, if you add the same appender to a category n times, that appender will be invoked n times to append to its target.

A slightly different cause is adding different appenders all sharing the same underlying output target to some category. In the most common occurrence of this phenomenon, the BasicConfigurator.configure() method is invoked multiple times. Each time it is invoked, this method adds an appender with a System.out target to the root category.

One other common mistake is to forget that appenders are inherited cumulatively from the hierarchy. For example, if you add an appender, say A, to the root category, all other categories will inherit A as an appender. Thus, if you add A to a category, say C, then an enabled statement of category C, will print to A twice, once because A is in root and once because it is in C.

1
  • don´t worry. it´s part of the "game". :) Commented May 25, 2012 at 14:07
3

I run into the same issue because of multiple Logger configuration using the same appender. Here is an example that would cause LOG4J to store some log messages twice:

log4j.logger.com.example = ERROR, out
log4j.logger.com.example.myapp = INFO, out

For example error messages within com.example.myapp.MyClass would be logged twice, because they match for both loggers.

To avoid this behavior you can disable Appender Additivity which is enabled by default:

log4j.additivity.com.example.myapp = false

See http://logging.apache.org/log4j/1.2/manual.html#additivity for more information.

0

In web application java creates seperate thread for each request so may be the function(in which you put logs)call multiple times and so generate log each time.

0

Please check the appenders you have provided in the log4j configuration file i.e. log4j.xml or log4j.properties . There might be multiple listed out in the configuration which is causing the logger to log the statements multiple times.

If this is not the case, please paste your configuration details here, so that we can have a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.