question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LoggerContext changes when using Spring Cloud

See original GitHub issue

Hi,

I am using the Spring Boot 1.5 micrometer code and have found that the LoggerContext changes shortly after application startup (which I believe is related to our use of Spring Cloud). When the application starts io.micrometer.core.instrument.binder.logging.LogbackMetrics registers io.micrometer.core.instrument.binder.logging.MetricsTurboFilter, but when the application context is reloaded, LogbackMetrics is now pointing to the old LoggerContext. To get round this I created the following class:

@Component
public class FlexLogbackMetricsListener implements ApplicationListener<ContextRefreshedEvent> {

    private final LogbackMetrics logbackMetrics;
    private final MeterRegistry meterRegistry;

    public FlexLogbackMetricsListener(final LogbackMetrics logbackMetrics, final MeterRegistry meterRegistry) {
        this.logbackMetrics = logbackMetrics;
        this.meterRegistry = meterRegistry;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
       logbackMetrics.bindTo(meterRegistry);
    }

}

Is there a better way to do this? It works a treat, but does seem a little hacky.

Thanks

Nick

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
nickcodefreshcommented, Apr 24, 2018

In the end this is what I managed to get working:

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
boolean exists = loggerContext.getTurboFilterList().stream()
    .anyMatch(i -> i.getClass().getName().equals("io.micrometer.core.instrument.binder.logging.MetricsTurboFilter"));
if (!exists) {
    logbackMetrics.bindTo(meterRegistry);
}
0reactions
hnibbrigcommented, Sep 29, 2020

Hi. Unfortunately spring-cloud/spring-cloud-commons#608 didn’t fix this. The original workaround still works. @shakuzen will you reopen this?

This issue would be reopened if there were some change to make in Micrometer itself. So far all the discussed solutions have been changes in other projects. The resolution of the linked spring-cloud-commons issue indicates to me that it should not be a problem since the 2020.0.0-M4 release of Spring Cloud.

You are completely right. I will head over to the spring-cloud-commons project and refer to this bug. Maybe they can help out some more. Thanks for the quick reply!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change logback log level dynamically in spring boot ...
With slf4j you can get the LoggerContext and change the level. LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); context.getLogger( ...
Read more >
Dynamically modifying Logger log levels in Spring Boot ...
Spring Boot uses logback as the logging framework by default. logback provides an interface to get logger and modify logger logging levels.
Read more >
5 Ways to Change Log Levels at Runtime - DZone DevOps
The user will use the spring boot admin UI to change the log levels. Merely select the logger and change the log level...
Read more >
Using Log4J 2 with Spring Boot - Spring Framework Guru
In this post, I will explain how to configure Spring Boot to use Log4J 2 over ... through application.properties, the bug changes the...
Read more >
How to allow user to make permanent changes to log levels?
You could create a new config file (outside the container) and use it to ... File file = new File("/config/new/log4j2.xml"); final LoggerContext ctx ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found