首页 文章

Spring Boot application.properties中日志级别的问题

提问于
浏览
1

我在Spring Boot应用程序中使用Log4J 2并尝试通过application.properties配置一些基本的日志记录选项 .

我在我的控制器类中设置了一个 Logger ,就像这样 .

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";
   }
}

在application.properties中,我有以下属性 .

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

当我启动应用程序时,Spring内部loggigng消息将在控制台中以INFO级别记录 . 这是从 logging.level.org.springframework.web 属性预期的 . 但是,当调用控制器时,仅记录 info() 和更低的消息 . 将 logging.level.guru.springframework.controllers 属性设置为DEBUG,为什么不记录 debug() 方法的消息 . 如果我将以下内容添加到application.properties中 .

logging.level.root=ERROR

现在,在Spring启动时没有消息被发送到控制台(显然此次错误级别已被提取) . 类似地,控制器仅发出 error() 方法消息 . 那么,是否有从application.properties中获取级别的特定优先级 .

有没有办法为不同的包(比如控制器包)设置不同的级别,而与application.properties中为 logging.level.rootlogging.level.org.springframework.web 指定的级别无关 .

我不想使用任何其他外部配置文件 .

1 回答

  • 1

    您正在使用包含a bug的旧版Spring Boot(1.2.4.RELEASE) . 它在1.2.6.RELEASE中修复,但我建议升级到1.2.8.RELEASE(如果你需要坚持使用1.2.x),或者更好的是,升级到1.3.3.RELEASE(最新版本在写作时间) . 您可以通过更新项目的pom中使用的 spring-boot-starer-parent 版本进行升级:

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

相关问题