Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException in SpringBoot Logging

I'm getting an issue when my application starts up. The application fails when configuring log levels, on a ClassCastException java.util.HashMap cannot be cast to org.springframework.boot.logging.LogLevel.

I've tried setting logging.level.root=INFO in my application.properties and the same error occurs. Not sure what else to try.

I've just upgraded it to SpringBoot 2.x, specifically using this dependency BOM:

    <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>

Please find the relevant parts of the dependency tree:

[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
[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

Here is the stacktrace:

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

logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
logging.level.com.my.package.name=error

logging levels map causing problem Clearly the problem here is the values in this levels map are hashmaps, instead of strings.... Not sure why they are that way though.

like image 294
xandermonkey Avatar asked Nov 01 '19 14:11

xandermonkey


People also ask

Which logging is used in spring boot?

Spring Boot uses Apache Commons Logging for internal logging but allows developers to configure the underlying log implementation. Various logging providers are supported through simple configuration. Spring Boot provides default configurations for Java Util Logging, Log4J2 and Logback.

Can you control logging with spring boot How?

Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available.

What is default logging level in spring boot?

By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a logging. file or logging. path property (for example, in your application.

Why loggers are used in spring boot?

Logging in spring boot is basically an API that provides tracing out of information along with a recording of any critical failure that might occur in the application during its run. Spring boot uses a common logging framework to implement all internal logging and leaves the log implementation open.


1 Answers

try setting the logging value to WARN and debug=true and have a check.

in your properties file set the values like this

logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error

in log4j.xml it will look like this

<root>
        <level value="WARN"/>
        <appender-ref ref="console" />
        <appender-ref ref="file"/>

</root>

for more details about logging in spring boot please read the below link https://howtodoinjava.com/spring-boot2/logging/spring-boot-logging-configurations/

like image 88
TanvirChowdhury Avatar answered Sep 30 '22 09:09

TanvirChowdhury