简体   繁体   中英

Issues with Log Levels in Spring Boot application.properties

I am using Log4J 2 in a Spring Boot application and trying to configure few basic logging options via application.properties.

I have setup a logger in my controller class, like this.

package guru.springframework.controllers;
- - - -
@Controller
public class IndexController {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());
  @RequestMapping("/")
  String index(){
    logger.debug("This is a debug message");
    logger.info("This is an info message");
    logger.warn("This is a warn message");
    logger.error("This is an error message");
    return "index";
   }
}

In application.properties, I have the following properties.

logging.level.guru.springframework.controllers=DEBUG
logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log

Spring internal loggigng messages are getting logged with INFO level in the console when I start the app. This is expected from the logging.level.org.springframework.web property. However, when the controller gets invoked, only messages of info() and lower gets logged. With the logging.level.guru.springframework.controllers property set to DEBUG, why isn't the message of the debug() method getting logged. If I add the following to application.properties.

logging.level.root=ERROR

Now, no messages are sent to console on Spring startup (Obviously the Error level has been picked up this time). Similarly the controller emits only the error() method message. So, is there any specific priority of picking up levels from application.properties.

Is there a way to set up different levels for a different package (Say, the controller package) independent of the level specified for logging.level.root and logging.level.org.springframework.web in application.properties.

I don't want to use any other external configuration file.

You are using an old version of Spring Boot (1.2.4.RELEASE) that contains a bug . It was fixed in 1.2.6.RELEASE, but I'd recommend upgrading to 1.2.8.RELEASE (if you need to stick with 1.2.x) or, even better, to 1.3.3.RELEASE (the latest version at the time of writing). You can upgrade by updating the version of spring-boot-starer-parent that you are using in your project's pom:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM