I would like to have two different log4j
loggers in my application, and for there to be no "overlap" between the content they write to their respect logs.
For example:
- Logger1 writes INFO events related to one set of system events
- Logger2 writes INFO events related to another set of system events
- No entry should appear in the log twice
My log4j.properties is as follows:
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.apache=DEBUG, stdout
log4j.logger.xdasLogger=DEBUG, xdas
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
log4j.appender.xdas=org.apache.log4j.ConsoleAppender
log4j.appender.xdas.layout=org.apache.log4j.PatternLayout
log4j.appender.xdas.layout.ConversionPattern=%d %-5p %c - %m%n
My Java code is as follows:
public static void main(String[] args) {
PropertyConfigurator.configure(Client.class
.getResource("/log4j.properties"));
xdasLogger = Logger.getLogger("xdasLogger");
logger = Logger.getLogger(Client.class);
logger.info("normal");
xdasLogger.info("xdas");
}
But my console output is as follows:
normal
2012-06-28 09:52:44,580 INFO xdasLogger - xdas
xdas
Note that "xdas" is logged by both logger
and xdasLogger
, which is undesirable.
Does anyone know what changes I need to put into my log4j.properties to change the console output to the following?
normal
2012-06-28 09:52:44,580 INFO xdasLogger - xdas
Solution (taken from accepted answer):
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.apache=DEBUG, stdout
log4j.logger.xdasLogger=DEBUG, xdas
log4j.additivity.org.apache=false
log4j.additivity.xdasLogger=false
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
log4j.appender.xdas=org.apache.log4j.ConsoleAppender
log4j.appender.xdas.layout=org.apache.log4j.PatternLayout
log4j.appender.xdas.layout.ConversionPattern=%d %-5p %c - %m%n