Spring配置日志级别报红:Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'logging.level' to java.util.Map

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 (for example, in 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.webDEBUG级别的。

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变量。