1
votes

I have a log4j configuration file with two loggers and two especific appenders. The file looks how is show below:

log4j.rootLogger=DEBUG, CONSOLE

Logger to be used with Logger.getLogger(Class) method
log4j.logger.foo.bar=ALL, LOGFILE

Logger to be used with Logger.getLogger(String) method
log4j.logger.foo.bar.dummy.pack.XML=ERROR, LOGERRORXML

log4j.appender.LOGERRORXML=org.apache.log4j.RollingFileAppender
log4j.appender.LOGERRORXML.append=true
log4j.appender.LOGERRORXML.encoding=UTF-8
log4j.appender.LOGERRORXML.file=Log_Error.log
log4j.appender.LOGERRORXML.MaxFileSize=52428800
log4j.appender.LOGERRORXML.MaxBackupIndex=10
log4j.appender.LOGERRORXML.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGERRORXML.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
log4j.appender.LOGERRORXML.Threshold=ERROR

log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.append=true
log4j.appender.LOGFILE.encoding=UTF-8
log4j.appender.LOGFILE.file=Main_Logger.log
log4j.appender.LOGFILE.MaxFileSize=52428800
log4j.appender.LOGFILE.MaxBackupIndex=10
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
log4j.appender.LOGFILE.Threshold=ERROR

And the class look like this

    package foo.bar.dummy.pack;

    public class Foo
    {
        private static final Logger LOG = Logger.getLogger(Foo.class);

        private static final Logger LOG_XML = Logger.getLogger(Foo.class.getPackage().getName()+".XML");
    }

    public static void main(String[] args)
    {
        LOG.error("Hello ");
        LOG_XML.error("World!");
    }

The result of this is:

Main_Logger.log

[ignore log4j pattern layout] Hello
[ignore log4j pattern layout] World!

Log_Error.log

[ignore log4j pattern layout] World!

My idea is just logging the events that i want in separated files but when i call the logger LOGERRORXML the output is in both files, i mean in the file declared for LOGERRORXML appender and LOGFILE appender even when i'm using the LOGERRORXML logger. I think it's for the hierarchy dependence in the declaration of LOGFILE logger.

How i can configure log4j for only print the output in a specific appender (file appender in this case), without hierarchy dependences?

1

1 Answers

1
votes

To avoid duplication of messages, you may need:

#Logger to be used with Logger.getLogger(Class) method
log4j.logger.foo.bar=ALL, LOGFILE
log4j.additivity.foo.bar=false

#Logger to be used with Logger.getLogger(String) method
log4j.logger.foo.bar.dummy.pack.XML=ERROR, LOGERRORXML
log4j.additivity.foo.bar.dummy.pack.XML=false

See more Log4j Tutorial: Additivity – what and why?