Published 01 Sep, 2022

Java - slf4j logger is not printing the log in console for springboot if wrapped under log.isDebugEnabled()

Category Java
Modified : Sep 29, 2022
99

I am using slf4j logger in my controller class, even though my application.properties has the following entries:

#logging.level.*= DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.com.ge.power.brs.controllers.*=DEBUG

The code looks like :

 private static final Logger LOGGER = LoggerFactory.getLogger(EventControllerV1.class);

public ResponseEntity<List<EventView>> requestEvents(@RequestParam("user_name") String userName) throws Exception { 
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("EventControllerV1:::requestEvents:::Parameters>>::userName::" + userName);
    }
    return new ResponseEntity<>(eventManager.findEvents(userName), HttpStatus.OK);
}

**

In the background slf4j uses Logback framework, but still I am not able to get the console output..but if I remove the condition and use log.info("xxx") only , I can see the output in console when I hit the endpoint

Answers

There are 2 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Logging, Spring, Spring Boot, Slf4j. These have been categorized in sections for a clear and precise explanation.

45
if(log.isDebugEnabled()){
  log.info("xxx") ;
}

Doesn't make much sense.

instead, it should be

if(log.isDebugEnabled()){
  log.debug("xxx") ;
}

And to answer your original question,

this is not working

if(log.isDebugEnabled()){
  log.info("xxx") ;
}

and this is working log.info("xxx") ;

then that means, Log level for that class and method is at INFO level (which is higher than DEBUG). Try removing the * from logging.level.com.ge.power.brs.controllers.*=DEBUG


4

Inside "src/main/resources/application.properties" file, add this:

logging.level.com.mypackage.name=INFO

So for example:

logging.level.com.yourbusiness=INFO

If you want it all, then try

logging.level.com=INFO

The ".com" part refers to anything inside src/main/java/com. So for my project, I have src/main/java/com/yourbusiness