6

I want to change the log level of a Spring Boot application that is running.

Is it possible to change the log level at runtime? Right now I have the logger configuration in my jar itself.

kryger
  • 12,906
  • 8
  • 44
  • 65
Prabhakar D
  • 1,084
  • 2
  • 15
  • 35
  • 1
    Check out the answer to this question here:http://stackoverflow.com/questions/5448673/slf4j-logback-how-to-configure-loggers-in-runtime – DanielM Aug 25 '15 at 05:10

3 Answers3

15

Changing the log level while the application is running is part of the underlying logger implementation.

You did not specify the logger implementation you are using so I will assume you are using the default logback provided via the spring-boot-starter-logging or spring-boot-starter-web dependencies.

Comment out any logger related configurations from application.properties e.g.

#logging.path=logs
#logging.level.org.springframework.web= INFO
#logging.level.=INFO

Add logback.xml in the root of your classpath with tag See http://logback.qos.ch/manual/jmxConfig.html

Start the application and open JConsole and go to MBeans tab. Select the package ch.qos.logback.classic.JMxConfigurator.Under default locate the setLoggerLevel operation e.g. org.springframework.web, DEBUG

enter image description here

The change will be effective immediately. For other logger libraries see the spring boot user guide http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html And library specific information e.g. for log4j http://www.sureshpw.com/2012/04/dynamic-logging-with-log4j.html

A different approach is to repeat the about steps without JMX and use configuration watcher

Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • Thank you for the detailed explanation. I am using logback so I can follow the approaches you have outlined above – Prabhakar D Aug 25 '15 at 12:21
  • this answer and http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html helped a lot. another option I can think of is autowiring `org.springframework.boot.logging.logback.LogbackLoggingSystem` and use `LogbackLoggingSystem.setLogLevel(String loggerName, LogLevel level)` to programmatically change the logging level at runtime. in this way, you can build your own rest api around it and control it however you want (just make sure you secure the endpoint in a proper way) – burcakulug Feb 01 '16 at 16:45
  • @Prabhakar D - Accept this as the answer rather than the other link only answer. – Mubin Jul 05 '16 at 13:04
3

If you want to change the logging level of an already running Spring Boot application you can take a look at spring-cloud-config. Refer to: http://cloud.spring.io/spring-cloud-config/:

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments.

You can centrally manage the properties in config server and in your current application - applications.properties file (check bootstrap.properties) create an entry for

spring.application.name=application name

Using @RefreshScope annotation in your client application you will be able to refresh your application runtime and see the updated logging level property.

kryger
  • 12,906
  • 8
  • 44
  • 65
Swetha V
  • 300
  • 5
  • 16
2

With the release of Spring Boot 1.5, if you have actuator in your Boot application you can do this through an out of the box REST API.

1.5 actuator provides an endpoint called 'loggers' which you can GET to view the configuration, and POST to make runtime changes.

Ex.

curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' http://localhost:8080/loggers/org.springframwork 
Yohan Liyanage
  • 6,840
  • 3
  • 46
  • 63