comparison content/Java/log4j-logger-additivity.md @ 98:1d9382b0329b

Specify the syntax on markdown blocks to avoid broken output that has class=err
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 19 Dec 2019 10:04:33 +0100
parents 4cd9b65e10e4
children
comparison
equal deleted inserted replaced
97:e99db3bc53c1 98:1d9382b0329b
3 Tags: log4j, logging 3 Tags: log4j, logging
4 Lang: en 4 Lang: en
5 5
6 Sometimes you want to write more than one logfile using logj4. This is possible by defining multiple appenders and specifying an appender for a certain logger like this: 6 Sometimes you want to write more than one logfile using logj4. This is possible by defining multiple appenders and specifying an appender for a certain logger like this:
7 7
8 :::shell
8 log4j.appender.A1=org.apache.log4j.ConsoleAppender 9 log4j.appender.A1=org.apache.log4j.ConsoleAppender
9 .... 10 ....
10 log4j.appender.A2=org.apache.log4j.FileAppender 11 log4j.appender.A2=org.apache.log4j.FileAppender
11 .... 12 ....
12 13
13 log4j.rootLogger=DEBUG, A1 14 log4j.rootLogger=DEBUG, A1
14 log4j.logger.foo=DEBUG, A2 15 log4j.logger.foo=DEBUG, A2
15 16
16 Unfortunately, all output that goes through the logger foo comes out in both appenders, which may not be what you want. The log4j docs talk about *logger additivity* but don't show concrete examples how to configure it. The trick is to configure the additivity **on the logger** and **not on the appender**. (I always fall into that trap). Simply add the following to the example above to stop messages to logger foo come out on A1: 17 Unfortunately, all output that goes through the logger foo comes out in both appenders, which may not be what you want. The log4j docs talk about *logger additivity* but don't show concrete examples how to configure it. The trick is to configure the additivity **on the logger** and **not on the appender**. (I always fall into that trap). Simply add the following to the example above to stop messages to logger foo come out on A1:
17 18
19 :::shell
18 log4j.additivity.foo = false 20 log4j.additivity.foo = false