springbootlearner
springbootlearner

Reputation: 1370

How to change logback log level dynamically in spring boot application

I have a Spring boot application which use logback.xml for logging configurations.I am looking for options to dynamically change log level. For instance if I have deployed an app with loglevel as ERROR,Let say I want to change this to INFO but I don't want to redeploy/restart my JVM.

Is there any possibility we can configure logback.xml like config server to achieve this

Upvotes: 6

Views: 9292

Answers (3)

Grinish Nepal
Grinish Nepal

Reputation: 3075

If you are using spring cloud then you can have this in your yml file

logging:
  level:
    root: INFO

Then you can change it and refresh the configuration using actuator refresh to fetch new configuration changes no need to restart the service.

Also if you need some sort of UI to do this stuff you can explore the Spring-cloud-dashboard It is pretty cool and uses the features from the actuator to do and show you a lot of stuff not only changing log levels.

Upvotes: 3

lawal
lawal

Reputation: 992

Yes, this is quite possible. Expose a rest endpoint where you supply the className and log level. With slf4j you can get the LoggerContext and change the level.

    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.getLogger(className).setLevel(Level.valueOf(level));

Apache Commons logging and others have similar features.

Upvotes: 4

luboskrnac
luboskrnac

Reputation: 24561

You can configure Logback to Automatically reloading configuration file upon modification

Upvotes: 3

Related Questions