Spring配置日誌級別報紅:Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String
在SpringBoot2.x.x
版本之後,在application.yml
配置檔案中配置了修改預設logging.level
(info)如下:
logging:
level: debug
然後報錯如下:
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String, org.springframework.boot.logging.LogLevel> at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:363) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:323) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:308) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:238) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:212) at org.springframework.boot.context.logging.LoggingApplicationListener.setLogLevels
原因:在新版本中,logging.level後面需要指定對應的 “logger-name”,可以預設設定為root。
解決方法,修改成下面的形式:
logging:
level:
root: debug
具體解釋詳見官方文件,官方文件 https://docs.spring.io/spring-boot/docs/2.2.3.RELEASE/reference/htmlsingle/#boot-features-custom-log-levels,說明如下:
All the supported logging systems can have the logger levels set in the Spring Environment
application.properties
) by using logging.level.<logger-name>=<level>where level
is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root
logger can be configured by using logging.level.root
.
通過使用logging.level,所有支援的日誌系統都可以在Spring環境(例如,在
application.properties
)中設定日誌程式級別。logging.level.<logger-name>=<level>
,其中level是TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF其中一個,可以使用logging.level.root
來配置root日誌。
The following example shows potential logging settings in application.properties
:
下面的示例顯示了application.properties中可能的日誌記錄設定:
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
It’s also possible to set logging levels using environment variables. For example, LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG
will set org.springframework.web
to DEBUG
.
還可以使用環境變數設定日誌級別。例如,可以使用
LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUGSPRINGFRAMEWORK_web
設定org.springframework.web
成DEBUG
級別的。
The above approach will only work for package level logging. Since relaxed binding always converts environment variables to lowercase, it’s not possible to configure logging for an individual class in this way. If you need to configure logging for a class, you can use the SPRING_APPLICATION_JSON
variable.
上面的方法只適用於包級水平的日誌記錄。由於鬆散繫結總是將環境變數轉換為小寫,因此不可能以這種方式為單個類配置日誌記錄。如果您需要為一個類配置日誌記錄,您可以使用
SPRING_APPLICATION_JSON
變數。