1

Here is what i have in my java code

private static final Logger SUCCESS = LogManager.getLogger("success");
private static final Logger ERROR = LogManager.getLogger("error");

And I need to create 2 log files for these logs. (i tried to follow Creating multiple log files of different content with log4j) but I think its not exactly the same. But here goes what i created in log4j.properties.

log4j.rootLogger=TRACE, SuccessAppender, ErrorAppender

# setup SuccessAppender
log4j.appender.SuccessAppender=org.apache.log4j.RollingFileAppender
log4j.appender.SuccessAppender.Threshold=INFO
log4j.appender.SuccessAppender.File=success.log
log4j.appender.SuccessAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.SuccessAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# setup ErrorAppender
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.Threshold=INFO
log4j.appender.ErrorAppender.File=error.log
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

I was wondering how to map this "success" log into the "SuccessAppender".

Community
  • 1
  • 1
dinesh707
  • 12,106
  • 22
  • 84
  • 134

2 Answers2

2

First of all you have to make sure that the RootLogger is not logging anything. This can be achieved in different ways but my first solution is to let it append to the NullAppender.

log4j.rootLogger=OFF, NullAppender

# setup NullAppender
log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender

Then you'll have to set the log level and the appender for your loggers (success and error):

log4j.logger.success = TRACE, SuccessAppender
log4j.logger.error = TRACE, ErrorAppender

And the rest of your log4j.properties is left as is.


An alternate solution if you don't want to use the NullAppender would be to set the additivity flag on the loggers:

log4j.rootLogger=OFF, SuccessAppender, ErrorAppender

log4j.logger.success = TRACE, SuccessAppender
log4j.additivity.success = false
log4j.logger.error = TRACE, ErrorAppender
log4j.additivity.error = false

Some more information about the additivity can be found in the Log4J manual.

Appender Additivity

The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".

However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.

Loggers have their additivity flag set to true by default.

Community
  • 1
  • 1
maba
  • 47,113
  • 10
  • 108
  • 118
  • 1
    Why do i need to turn off root logger ? – dinesh707 Aug 13 '14 at 08:49
  • 1
    Otherwise you might get multiple outputs into your files. This is because the appender associated with logger `success` is first used, which writes the first instance to the `SuccessAppender`. Next, the parent of `success`, which in this case is the root logger, is referenced. The event is then passed to its appender, which is also writes to the SuccessAppender, resulting in the second instance. – maba Aug 13 '14 at 08:52
  • 1
    I added an alternate solution if you didn't like the idea of using a the NullAppender. – maba Aug 13 '14 at 08:58
0

try

 log4j.logger.success=TRACE, SuccessAppender
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64