apache / logging-log4j2

Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
https://logging.apache.org/log4j/2.x/
Apache License 2.0
3.4k stars 1.62k forks source link

NPE when using `rootLogger` shorthand properties notation #3206

Open ayush11-96 opened 2 weeks ago

ayush11-96 commented 2 weeks ago

Currently we are using log4j2 version 2.17.0 with spring boot but while updating log4j2 to 2.17.2 we are getting below error and which is because of the appender of root is initialise to null Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because the return value of "org.apache.logging.log4j.core.config.LoggerConfig.getAppenderRefs()" is null

We are passing below properties:

rootLogger.appenderRef.console.ref=console
rootLogger.appenderRefs=console
rootLogger=INFO

Please help us to understand if we require some changes along with version upgrade?

ppkarwasz commented 2 weeks ago

@ayush11-96,

Does the problem also occur with version 2.24.1? Can you provide the full stack trace of the exception?

ayush11-96 commented 2 weeks ago

@ppkarwasz I tried till 2.23.1 version and this is still happening Stack Trace:

Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because the return value of "org.apache.logging.log4j.core.config.LoggerConfig.getAppenderRefs()" is null
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:740) ~[log4j-core-2.23.1.jar:2.23.1]
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:264) ~[log4j-core-2.23.1.jar:2.23.1]
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313) ~[log4j-core-2.23.1.jar:2.23.1]
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631) ~[log4j-core-2.23.1.jar:2.23.1]
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:292) ~[log4j-core-2.23.1.jar:2.23.1]
ayush11-96 commented 2 weeks ago

@ppkarwasz could u please elaborate the issue if u got that?

ppkarwasz commented 2 weeks ago

@ayush11-96,

There is problem in your configuration: you both use the shorthand rootLogger = <level>, <ref1>, <ref2> notation that was introduced in Log4j Core 2.17.2 IIRC (see Properties configuration quirks for more information) and the normal rootLogger.appenderRef.<n>.ref properties. You should either use the long notation:

rootLogger.level = INFO
rootLogger.appenderRef.0.ref = console

or the shorthand notation (which only exists in the Java properties format):

rootLogger = INFO, console

The NPE is due to the fact that the current code does not handle the case, when the shorthand notation is used and no appender reference is provided (rootLogger = INFO). We will fix this in the next release.

ayush11-96 commented 2 weeks ago

Thanks @ppkarwasz Can I expect any ETA for the new version with fix?

ppkarwasz commented 2 weeks ago

Thanks @ppkarwasz Can I expect any ETA for the new version with fix?

This bug only affects very unlikely configuration scenario, when a user does not want any appenders for the root logger. It is not a priority.

Your configuration triggered the bug, because you have a typo in your configuration: replace rootLogger = INFO with rootLogger.level = INFO and everything will work. See my comment above for two variants of a working configuration.

ayush11-96 commented 2 weeks ago

thanks @ppkarwasz

ppkarwasz commented 2 weeks ago

I keep this open, so we can fix the NPE in the rare case a user really does not want any appenders attached to the root logger.