Home > Net >  Can I change the level of a message logged by Spring?
Can I change the level of a message logged by Spring?

Time:01-11

Our Spring-based microservice sometimes fails to connect to its eventing service for a couple of minutes. This is okay from an application point of view, as we have several retries and fallbacks in place that take care of this. Each failed connection attempt results in an ERROR log written by the class org.springframework.jms.listener.DefaultMessageListenerContainer. Is it possible to configure Spring such that this log message would be written with a lower log level, such as WARN?

CodePudding user response:

As @Jens mentioned this is not possible since the level at which a message is logged is given by the actual function call.

So, if Spring (or any other library or even your own code, really) has something like:

public void connect() {
    try(establishConnection()) {
        // ...
    } catch (Exception e) {
        log.error("Failed to establish connection");
    }

you'll notice that the logging is always done at the error level, since that is what is being invoked.

The only recourse you have in this case is to set the log level of the class to OFF if your given logging implementation supports it (the default, Logback, does). For example:

# application.yml
logging.level:
  org.springframework.jms.listener.DefaultMessageListenerContainer: OFF

However, there's really no reason to do this unless you are getting absolutely swamped by these log messages (and even then, just filter them in your Dashboard). Error logs exist for a reason.

Alternatively, I suppose you could also do some hacky trickery using AspectJ to change the level something is logged at if you really, really needed to, but I doubt this applies in this case.

Lastly, you can fork the library and link your own Jar files into your application. But I doubt this maintenance burden is worth it in this case.

CodePudding user response:

As @filpa mentioned this is not possible because the level at which a message is logged is given by the actual function call. .

So, if Spring (or any other library or even your own code, really) has something like:

public void connect() {
try(establishConnection()) {
    // ...
} catch (Exception e) {
    log.error("Failed to establish connection");
}

you'll notice that the logging is always done at the error level, since that is what is being invoked.

The only recourse you have in this case is to set the log level of the class to OFF if your given logging implementation supports it (the default, Logback, does). For example :

# application.yml

logging.level: org.springframework.jms.listener.DefaultMessageListenerContainer: OFF

However, there's really no reason to do this unless you are getting absolutely swamped by these log messages (and even then, just filter them in your Dashboard). Error logs exist for a reason.

Furthermore, you may fork the library and link your own Jar files into your application. But I doubt this maintenance burden is worth it in this case.

  • Related