Skip to content

Improve Log4j Core Configuration File Detection for Version 3 #46409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ppkarwasz
Copy link
Contributor

Log4j Core 3 has undergone significant modularization and no longer uses optional parser dependencies. This change requires updates to Spring Boot's configuration file detection logic to properly support both Log4j Core 2 and 3.

Summary of Changes

  • Updated configuration file detection
    Spring Boot now detects configuration formats also based on the presence of ConfigurationFactory implementations, instead of only relying on optional parser dependencies (as was the case in Log4j Core 2).

  • Improved classloader usage for reflection
    Reflection logic now uses the classloader that loaded Log4j Core, rather than the one associated with the Spring Boot context, ensuring greater compatibility in modular environments.

  • Adjusted configuration file lookup order
    The lookup now prioritizes configuration files specified via properties over automatically discovered ones, improving consistency with Log4j Core.

  • Support for contextual configuration files
    Files named in the form log4j2<contextName>.<extension> are now also supported.

These changes ensure compatibility with Log4j Core 3 while preserving support for Log4j Core 2, improving Spring Boot's flexibility in detecting and loading user-defined logging configurations.

Note

The configuration file detection logic introduced here could potentially be moved into a future version of Log4j Core itself. For more context, see apache/logging-log4j2#3775.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 12, 2025
Log4j Core 3 has undergone significant modularization and no longer uses optional parser dependencies. This change requires updates to Spring Boot's configuration file detection logic to properly support both Log4j Core 2 and 3.

* **Updated configuration file detection**
  Spring Boot now detects configuration formats based on the presence of `ConfigurationFactory` implementations, instead of relying on optional parser dependencies (as was the case in Log4j Core 2).

* **Improved classloader usage for reflection**
  Reflection logic now uses the classloader that loaded Log4j Core, rather than the one associated with the Spring Boot context, ensuring greater compatibility in modular environments.

* **Adjusted configuration file lookup order**
  The lookup now prioritizes configuration files specified via properties over automatically discovered ones, improving consistency with Log4j Core.

* **Support for contextual configuration files**
  Files named in the form `log4j2<contextName>.<extension>` are now also supported.

These changes ensure compatibility with Log4j Core 3 while preserving support for Log4j Core 2, improving Spring Boot's flexibility in detecting and loading user-defined logging configurations.

> [!NOTE]
> The configuration file detection logic introduced here could potentially be moved into a future version of Log4j Core itself. For more context, see apache/logging-log4j2#3775.

Signed-off-by: Piotr P. Karwasz <[email protected]>
@ppkarwasz ppkarwasz force-pushed the feat/log4j-core-3-configuration branch from 0de4595 to 2c40cad Compare July 12, 2025 17:58
Signed-off-by: Piotr P. Karwasz <[email protected]>
/**
* Configuration factory for YAML files (Log4j 3, optional dependency).
*/
private static final String YAML_CONFIGURATION_FACTORY_V3 = "org.apache.logging.log4j.config.yaml.YamlConfigurationFactory";
Copy link

@ronodhirSoumik ronodhirSoumik Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldnt it better to keep those in log4j.properties file and use that here to make it more rebust

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fully qualified class names of the Log4j configuration factories and parsers are stable and unlikely to change, so I don't see a clear advantage in externalizing them to a Java properties file. Doing so would add complexity without providing much practical benefit in this context.

For a more robust and dynamic approach, it is possible to query Log4j’s ConfigurationFactory system directly to discover active factories and their supported file types. However, this mechanism would require relying on more Log4j internal classes and the way to retrieve all the ConfigurationFactorys changes between version 2.x and 3.x.

Note: If Spring Boot 4.x ends up depending on Log4j 2.26.x or newer, we could leverage the enhancements introduced in apache/logging-log4j2#3775 to make this process simpler and more robust. However, given the current timeline, it's unclear whether we will be able to finalize the release in time for inclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants