0

A typical spring boot application sends INFO level messages to console. I know this root logger level can be changed in a logback xml config file. I have a need to change it through the spring boot application itself.

I am using spring boot2 with no logback xml file in my resources. With these lines in the class annotated

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.Level;

public static void main(String[] args) {
     ((Logger)org.slf4j.LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.DEBUG);
     ...
}

with @SpringBootApplication, when I run the application, for some reason I am not seeing DEBUG messages.

user3477071
  • 194
  • 1
  • 13
  • Hi, just a couple of questions, are you sure your application is using this logback? from what I remember spring boot is using one logger depending of what you have on your classpath. Are you interested by having a way to achieve this with properties files and not programatically ? – EFOE Feb 15 '20 at 20:12
  • do you see any error while running application? – Girdhar Singh Rathore Feb 15 '20 at 20:19
  • No error when running application. When I run 'java -jar ', I don't see any debug messages. My only spring boot dependency is org.springframework.boot:spring-boot-starter-web. Possibly spring boot's default logback xml file configuration at org/springframework/boot/logging/logback/base.xml is prevailing over what I set through Java. I would like to get this working through Java. – user3477071 Feb 15 '20 at 20:21
  • try using ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); root.setLevel(level); – Girdhar Singh Rathore Feb 15 '20 at 20:26
  • or checkout first answer from https://stackoverflow.com/questions/3837801/how-to-change-root-logging-level-programmatically-for-logback – Girdhar Singh Rathore Feb 15 '20 at 20:28
  • @Giridhar My Logger.ROOT_LOGGER_NAME was also a ch.qos.logback.classic.Logger. I did not include that import in my question. I copy pasted your code and it did not help me get DEBUG messages. Also, the provided link did not help me. – user3477071 Feb 15 '20 at 20:34

0 Answers0