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:
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
Try setting the additivity of the loggers to false. This will avoid the propagation to the rootLogger.
log4j.additivity.org.apache=false
log4j.additivity.xdasLogger=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