Home » Java » Log4j2 configuration – What happens if AppenderRef not set for logger?

Log4j2 configuration – What happens if AppenderRef not set for logger?

Posted by: admin December 28, 2021 Leave a comment

Questions:

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>

.
.

Answers:

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.

###

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.