-
Notifications
You must be signed in to change notification settings - Fork 41k
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
Support optional: prefix with logging.log4j2.config.override #44399
Comments
I am not sure that is a good way to go, there could be a lot of people who rely on I believe a good compromise would be to add support for the logging:
label: ${spring.cloud.config.label:master}
config: "https://spring-configuration-server.acme.org/some-service/default/${logging.label}/log4j2-json-dev.xml"
log4j2:
config:
override: "optional:https://spring-configuration-server.acme.org/some-service/default/${logging.label}/log4j2-some-service-dev.xml"
I've added |
Introduced support for the 'optional:' prefix in Log4j2 override file locations, ensuring missing files are ignored without throwing exceptions. See spring-projectsgh-44399 Signed-off-by: Dmytro Nosan <[email protected]>
I would expect that many users using this feature were using log4j-spring-boot, so removing that jar exposes this behavior. IOW, the breaking change was done in Spring Boot. However, I am open to making a change similar to what you propose but I don't think changing the URI string is the appropriate way to do it. I would expect it to be something like:
or
|
There's support elsewhere in Boot for using |
Reading through the issue again, I think it's important to be able to distinguish between an override that's present yet broken and an override that's not there. I think the former should cause a failure while the latter should not. This would be consistent with, for example, our handling of |
Agreed, that's why I chose not to include a |
If using optional: as a prefix is the approved way of handling this kind of thing in Spring then I am fine with the proposed solution. |
Unfortunately, it is not possible to have the same behaviour with Log4j2. If the configuration file is invalid, Log4j2 does not throw an exception. @Test
@WithNonDefaultXmlResource
@WithResource(name = "override.xml", content = """
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"
""")
void loadOptionalOverrideConfigurationWhenConfigurationIsInvalid() {
this.environment.setProperty("logging.log4j2.config.override", "optional:classpath:override.xml");
//Exception is not thrown here.
assertThatException().isThrownBy(
() -> this.loggingSystem.initialize(this.initializationContext, "classpath:nondefault.xml", null));
} JsonConfiguration/YamlConfiguration BuiltConfiguration/PropertiesConfiguration |
Introduced support for the 'optional:' prefix in Log4j2 override file locations, ensuring missing files are ignored without throwing exceptions. See spring-projectsgh-44399 Signed-off-by: Dmytro Nosan <[email protected]>
Introduced support for the 'optional:' prefix in Log4j2 override file locations, ensuring missing files are ignored without throwing exceptions. See spring-projectsgh-44399 Signed-off-by: Dmytro Nosan <[email protected]>
Introduced support for the 'optional:' prefix in Log4j2 override file locations, ensuring missing files are ignored without throwing exceptions. See spring-projectsgh-44399 Signed-off-by: Dmytro Nosan <[email protected]>
I opened PR: #44488 but then started wondering if it is the best solution for this issue. Perhaps in Spring Boot 4.x, the Log4j2 override configuration loading could be revisited to align In that case, The remaining question is: how should this issue be handled in |
It seems that
On the other hand, if exceptions are caught and logged to |
Log4j2LoggingSystem contains the following method:
If an error occurs loading an override file the exception will percolate to the catch block and fail loading the logging configuration. The log4j-spring-boot module provided by Log4j had:
So with log4j-spring-boot a warning message would be logged that the override file couldn't be loaded but configuration would still take place with the primary config file.
This behavior in Log4j2 was intentional. All the services I work with specify a shared primary logging configuration and a per application override location such as
If the application doesn't need to override anything then it just uses the primary configuration. If we want to add an override we can just add it to Spring Cloud Config without having to change the application.
In short, the configurations.add() call needs to be placed in its own try/catch block that logs a similar message to what log4j-spring-boot was doing and ignore the failure.
The text was updated successfully, but these errors were encountered: