8

What happens if I don't explicitly set the AppenderRef for the logger config in log4j2.xml ? Would it use all the appenders by default ?

. .

<Logger name="com.package1" level="error"/>

<Root level="error">
  <AppenderRef ref="LOGFILE"/>
  <AppenderRef ref="CONSOLE"/>
</Root>

. .

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Sumalatha Abhishek
  • 641
  • 1
  • 8
  • 16

2 Answers2

15

A logger calls all appenders for which it has an AppenderRef, and after that it delegates to its parent (the root logger in your example).

You can prevent it from calling its parent by configuring additivity="false". In that case the level configured on the logger may prevent the event from being logged. This is commonly used to filter out very verbose logging from certain packages.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
5

In addition to Remko's answer, if the Logger with additivity set to false or the root Looger has no appender references then the event will be ignored.

rgoers
  • 8,696
  • 1
  • 22
  • 24