[Solved] java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext


ClassCastException throws in code has attempted to cast an object to a subclass of which it is not an instance.

  • ClassCastException():  Constructs a ClassCastException with no detail message.
  • ClassCastException( String s):  Constructs a ClassCastException with specified detail message.

Exception Stack Trace

Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
    at org.apache.logging.log4j.core.LoggerContext.getContext(LoggerContext.java:190) ~[log4j-core-2.10.0.jar:2.10.0]
    at org.apache.logging.log4j.core.config.Configurator.setLevel(Configurator.java:291) ~[log4j-core-2.10.0.jar:2.10.0]
    at org.elasticsearch.common.logging.Loggers.setLevel(Loggers.java:149) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.common.logging.Loggers.setLevel(Loggers.java:144) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.index.SearchSlowLog.setLevel(SearchSlowLog.java:111) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.index.SearchSlowLog.(SearchSlowLog.java:106) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.index.IndexModule.(IndexModule.java:127) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.indices.IndicesService.createIndexService(IndicesService.java:440) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.indices.IndicesService.createIndex(IndicesService.java:413) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:378) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:45) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.service.ClusterService.executeTasks(ClusterService.java:634) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.service.ClusterService.calculateTaskOutputs(ClusterService.java:612) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.service.ClusterService.runTasks(ClusterService.java:571) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.service.ClusterService$ClusterServiceTaskBatcher.run(ClusterService.java:263) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:150) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:188) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:575) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:247) ~[elasticsearch-5.6.10.jar:5.6.10]
    at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:210) ~[elasticsearch-5.6.10.jar:5.6.10]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:1.8.0_151]
    at java.lang.Thread.run(Unknown Source) ~[na:1.8.0_151]

Issue

This exception “org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext” occurs because project build with maven and contains spring-boot-starter-web which imports the dependency because we can’t skip with spring-boot-starter-web and need log4j.

Solutions 1

Solution to get log4j with web project is to exclude log4j-to-slf4 from class path as given below:


    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <exclusion>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-to-slf4j</artifactId>
    </exclusion>
    </exclusions>
</dependency>

Solutions 2

If you are using simple Spring boot console based application or any Spring template and interfacing with this exception.


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.0.2</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j;/artifactId>
<version>2.0.2-sources;/version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl;/artifactId>
<version>2.0.2;/version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl;/artifactId>
<version>2.0.2-sources;/version>
</dependency>

More Issues Solution

For more other JAVA/JDBC issues solution follow link JAVA/JDBC Issues.

Advertisements

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 11 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Exception, Java, Spring and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s