首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot日志记录中的ClassCastException

SpringBoot日志记录中的ClassCastException
EN

Stack Overflow用户
提问于 2019-11-01 22:00:34
回答 2查看 573关注 0票数 7

当我的应用程序启动时,我得到了一个问题。在ClassCastException java.util.HashMap cannot be cast to org.springframework.boot.logging.LogLevel上配置日志级别时,应用程序会失败。

我尝试在我的application.properties中设置logging.level.root=INFO,同样的错误也出现了。不知道还能尝试什么。

我刚刚将它升级到了SpringBoot 2.x,特别是使用了以下依赖项BOM:

代码语言:javascript
复制
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

请找到依赖关系树的相关部分:

代码语言:javascript
复制
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.0.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.28:compile
[INFO] |  |  +- org.springframework:spring-core:jar:5.2.0.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-jcl:jar:5.2.0.RELEASE:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.25:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.0:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.10.0:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.0:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- jakarta.validation:jakarta.validation-api:jar:2.0.1:compile
[INFO] |  |  \- org.hibernate.validator:hibernate-validator:jar:6.0.17.Final:compile
[INFO] |  |     \- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
[INFO] |  +- org.springframework:spring-web:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.2.0.RELEASE:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
[INFO] |     +- org.springframework:spring-context:jar:5.2.0.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.0.RELEASE:provided
[INFO] |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.27:provided
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.27:provided
[INFO] |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.27:provided
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:2.2.0.RELEASE:compile
[INFO] |  \- io.micrometer:micrometer-core:jar:1.3.0:compile
[INFO] |     +- org.hdrhistogram:HdrHistogram:jar:2.1.11:compile
[INFO] |     \- org.latencyutils:LatencyUtils:jar:2.0.3:compile
代码语言:javascript
复制
[INFO] |  |  +- org.springframework.cloud:spring-cloud-starter-oauth2:jar:2.0.0.RELEASE:compile
[INFO] |  |  |  +- org.springframework.cloud:spring-cloud-starter-security:jar:2.0.0.RELEASE:compile
[INFO] |  |  |  |  +- org.springframework.cloud:spring-cloud-starter:jar:2.0.0.RELEASE:compile
[INFO] |  |  |  |  |  +- org.springframework.cloud:spring-cloud-context:jar:2.0.0.RELEASE:compile
[INFO] |  |  |  |  |  |  \- org.springframework.security:spring-security-crypto:jar:5.2.0.RELEASE:compile
[INFO] |  |  |  |  |  +- org.springframework.cloud:spring-cloud-commons:jar:2.0.0.RELEASE:compile
[INFO] |  |  |  |  |  \- org.springframework.security:spring-security-rsa:jar:1.0.5.RELEASE:compile
[INFO] |  |  |  |  \- org.springframework.cloud:spring-cloud-security:jar:2.0.0.RELEASE:compile
[INFO] |  |  |  |     \- org.springframework.boot:spring-boot-starter-security:jar:2.2.0.RELEASE:compile
[INFO] |  |  |  \- org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:jar:2.0.0.RELEASE:compile
[INFO] |  |  |     \- org.springframework.security:spring-security-jwt:jar:1.0.9.RELEASE:compile
[INFO] |  |  |        \- org.bouncycastle:bcpkix-jdk15on:jar:1.56:compile
[INFO] |  |  |           \- org.bouncycastle:bcprov-jdk15on:jar:1.56:compile

下面是堆栈跟踪:

代码语言:javascript
复制
Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to org.springframework.boot.logging.LogLevel
    at java.util.HashMap.forEach(HashMap.java:1289)
    at org.springframework.boot.context.logging.LoggingApplicationListener.setLogLevels(LoggingApplicationListener.java:398)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeFinalLoggingLevels(LoggingApplicationListener.java:337)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:288)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:245)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:222)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)

application.properties

代码语言:javascript
复制
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
logging.level.com.my.package.name=error

很明显,这里的问题是这个levels映射中的值是散列映射,而不是字符串……不过,我不确定他们为什么会这样。

EN

回答 2

Stack Overflow用户

发布于 2019-11-04 14:21:28

尝试将日志记录值设置为WARN和debug=true,然后进行检查。

在您的属性文件中,像这样设置这些值

代码语言:javascript
复制
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error

log4j.xml中,它将如下所示

代码语言:javascript
复制
<root>
        <level value="WARN"/>
        <appender-ref ref="console" />
        <appender-ref ref="file"/>

</root>

有关登录spring boot的更多详细信息,请阅读以下链接https://howtodoinjava.com/spring-boot2/logging/spring-boot-logging-configurations/

票数 1
EN

Stack Overflow用户

发布于 2019-11-05 10:32:43

切换到logback,它可以让你更好地控制你的日志级别和附加器。请参阅https://dzone.com/articles/configuring-logback-with-spring-boot

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58660493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档