sromit sromit - 29 days ago 7
Java Question

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

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

Answer
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