Repository navigation
Description
The Log4j 1 bridge's XmlConfiguration creates every top-level <appender> element, whether or not a logger references it. This has been the case since the bridge was added in 57bfd48 (LOG4J2-63). Log4j 1 did not do this: DOMConfigurator.parse explicitly skips <appender> elements and creates an appender only when an <appender-ref> points to it. The bridge's PropertiesConfiguration also only creates appenders by reference, like Log4j 1's PropertyConfigurator.
Consequences
- Unused appenders still run: an appender that no logger references is still created and started. For example, a
FileAppendercreates its file and aSocketAppenderopens its connection, which Log4j 1 never did for the same configuration. - Unused appenders still report errors: an unreferenced appender whose class cannot be loaded logs errors. Log4j 1 ignored it. Before fix: null-check parseAppender result in log4j-1.2-api XmlConfiguration #4172 it was worse: such an appender caused an NPE that discarded the whole configuration.
- Unnamed appenders get registered: an
<appender>without anameattribute is registered under the name"". The DTD requiresname, and Log4j 1 could never create such an appender, because no<appender-ref>can point to it.
Proposed fix
Create appenders only by reference, like PropertiesConfiguration:
- In
findAppenderByName, register a newly created appender withaddAppender(AppenderAdapter.adapt(appender)). This is needed becauseparseChildrenOfLoggerElementlooks the appender up throughgetAppender(refName). - In
doConfigure, ignore top-level<appender>elements, asDOMConfiguratordoes.
This also makes the guards added in #4172 unnecessary.
Tests
- An unreferenced appender with a valid class is not present in the
Configuration. - An unreferenced appender with a class that cannot be loaded produces no error.
- A referenced appender still works, whether its
<appender>element comes before or after the logger that references it.
Compatibility
Users who relied on unreferenced appenders being created would lose them. For example, they may look an appender up through Configuration.getAppender(name), or reference it from Log4j 2 code. That only worked by accident and never worked in Log4j 1, so I would target 2.x (the next minor release) rather than a patch release.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTo triage
Activity