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

Issues with Log Levels in Spring Boot application.properties

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

我在我的控制器 class 中设置了一个记录器,就像这样。

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 中获取关卡是否有任何特定的优先级?

有没有办法为不同的包(比如控制器包)设置不同的级别,独立于为 logging.level.root 和 [=29 指定的级别=]logging.level.org.springframework.web 在 application.properties。

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

您正在使用包含 a bug 的旧版本 Spring 启动 (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>