簡體   English   中英

Spring Boot application.properties 中的日志級別問題

[英]Issues with Log Levels in Spring Boot application.properties

我在 Spring Boot 應用程序中使用 Log4J 2 並嘗試通過 application.properties 配置幾個基本的日志記錄選項。

我在我的控制器類中設置了一個記錄器,就像這樣。

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 內部日志消息在控制台中以 INFO 級別記錄。 這是logging.level.org.springframework.web屬性所預期的。 但是,當控制器被調用時,只會記錄info()和 lower 的消息。 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指定的級別。

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

您正在使用包含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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM