45

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
Alex Averbuch
  • 3,245
  • 5
  • 33
  • 44

1 Answers1

45

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
kjp
  • 3,086
  • 22
  • 30
  • Your solution seems absolutely correct but isn't working for me. I added these lines to `/etc/spark/conf/log4j.properties`, is that not correct? Do I need to restart `Spark` (or reset anything else) so that updated properties are picked up? – y2k-shubham Feb 22 '18 at 12:33
  • Did you use the logger names already configured in the log4j.properties for Spark? Or maybe Spark uses log4j2 which has a different property file - log4j2.properties? If you can give more details, I can try to help – kjp Mar 10 '18 at 02:08
  • **@kjp** as far as I know, `Spark 2.3.0` (released 28th Feb, 2018) still uses `log4j` as told [here](https://spark.apache.org/docs/latest/configuration.html#configuring-logging). Additionally, what do you mean by *"logger names already configured in the log4j.properties for Spark"*? In the `log4j.properties` files I see lines like these `log4j.rootCategory=WARN,console` & `log4j.appender.console=org.apache.log4j.ConsoleAppender` – y2k-shubham Mar 10 '18 at 05:23
  • Can you give a link to the full log4j config? – kjp Mar 21 '18 at 13:42
  • **@kjp** [here](https://drive.google.com/open?id=19aQVQtKafd7ZKaHsAKNm9luX5FcOLAKi)'s the link to my `/etc/spark/conf.dist/log4j.properties` file. Although I've downgraded to `Spark 2.2.1` (still on `EMR 5.12.0`), that doesn't seem to have any impact on this issue. – y2k-shubham Mar 21 '18 at 13:57