-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rebind Log4j2Metrics
when LoggerContext#reconfigure
is called
#5756
Comments
I'm not familiar with how the details of how reconfiguring works. Could you share a snippet of code demonstrating what is being done? Ideally if it can be put in a unit test that is minimal (i.e. doesn't involve Spring), we can add it to our tests when we address this and ensure it works now and going forward. I wonder if there is a way we can register a callback that is run when reconfiguration happens. |
I just change some config (any) at Nacos, and that will trigger a RefreshEvent of Spring. Then, the logging system of Spring will load loadConfiguration again.
` |
I can try to make some tests about this |
Adding a listener of PropertyChangeEvent may be a solution |
I add some code as below,and it works for my project. The problem is involved by Spring's Refresh Event, I cannot give some unit tests.
|
Hi, We have a similar issue with the log4j2 metrics. In our log4j2 configuration file, we use Here is a failing testcase that I believe recreates the issue. @Test
void doesNotRebindWhenConfigurationIsReloaded() {
MeterRegistry registry = new SimpleMeterRegistry();
LoggerContext context = (LoggerContext) LogManager.getContext(false);
try (Log4j2Metrics metrics = new Log4j2Metrics(emptyList(), context)) {
metrics.bindTo(registry);
Logger logger = context.getLogger(getClass());
Configurator.setLevel(logger, Level.INFO);
logger.info("first");
// This will reload the configuration, metrics will not be rebound and the counter will not be incremented
context.reconfigure();
logger.info("second");
// This equals to 1 because the metrics are not rebound
assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(2);
}
} I think there might be something that can be done using log4j2 configuration listeners. |
Log4j2Metrics
when LoggerContext#reconfigure
is called
@pativa thank you for the unit test to reproduce this. If you have an idea for a good solution, feel free to send a pull request. We would be happy to review. I have marked this issue as |
Sure, I checked it out and think I have a working fix. I'll create a PR! |
I realized while the proposed solution solves the case that I posted about (reconfiguring the logger context), it does not actually solve the original problem in this issue (configuring a logger through Spring Boot). The solution in my PR does not work for programatically added loggers after the context is set up, which is what Spring Boot does, see https://github.com/philwebb/spring-boot/blob/086d289636bfa009cb4ae9dd6587e0d8df3c405c/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java#L233 The problem is that I can make another PR for this if you want, after #5822 is merged. |
As far as I understand, in the original case, the configuration will be re-loaded, but perhaps there are other cases where it won't. We can take it up as a separate issue/additional enhancement. |
Describe the bug
Now,log4j2Metrics is registed when server starting up. But when Log4j2 LoggerContexted is reconfigured because of config being changed, the filter will not work.
Environment
To Reproduce
How to reproduce the bug:
After a spring server starts up, we change some log4j2's config, logging level for example and Log4j2's reconfigure will be triggered. After this, the filter in log4j2Metrics will not work.
Expected behavior
log4j2Metrics always work
The text was updated successfully, but these errors were encountered: