Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class org.apache.logging.slf4j.Log4jLogger cannot be cast to class ch.qos.logback.classic.Logger #723

Closed
notxndy opened this issue Oct 30, 2023 · 8 comments
Assignees

Comments

@notxndy
Copy link

notxndy commented Oct 30, 2023

This bug(?) appears in later versions of ch.qos.logback: classic > 1.2.12

I am using the below code in a unit test class with the following imports and declarations

`
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.slf4j.LoggerFactory;

private final Logger logger = (Logger) LoggerFactory.getLogger(MyClass.class);
private final ListAppender listAppender = new ListAppender<>();

@BeforeEach
public void setUp() {
    listAppender.start();
    logger.addAppender(listAppender);

}
`

I am using org.slf4j:api version 2.0.9 and logback-classic version 1.2.12 as that's the last version that doesn't break the casting.

Hoping I can get an answer or a guide to this issue.

@ceki
Copy link
Member

ceki commented Oct 30, 2023

SLF4J 2.x requires logback 1.3 or later.

@notxndy
Copy link
Author

notxndy commented Oct 30, 2023

Right but 1.3 and above breaks the casting.

@ceki
Copy link
Member

ceki commented Oct 30, 2023

It should not. What is the exception?

@notxndy
Copy link
Author

notxndy commented Oct 30, 2023

As the title reads: java.lang.ClassCastException: class org.apache.logging.slf4j.Log4jLogger cannot be cast to class ch.qos.logback.classic.Logger

Edit: With the following in my maven pom file dependencymanagement:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.9</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

@ceki
Copy link
Member

ceki commented Oct 30, 2023

org.apache.logging.slf4j.Log4jLogger ships with log4j-slf4j-impl module, part of log4j 2.x project. Thus, you surely have log4j-slf4j-impl.jar in your class path which is causing the cast exception.

@ceki ceki self-assigned this Oct 30, 2023
@ceki ceki added the NOT A BUG label Oct 30, 2023
@notxndy
Copy link
Author

notxndy commented Oct 30, 2023

I see such a jar is compiled under the following dependency

`[INFO] - org.springframework.boot:spring-boot-starter-log4j2:jar:2.7.11:compile

[INFO] +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.18.0:compile`

Will exclude it and post results.

@notxndy notxndy closed this as completed Oct 30, 2023
@notxndy
Copy link
Author

notxndy commented Oct 30, 2023

For some reason I wasn't able to exclude from the dependency tree. But I'm sure this is still an acceptable answer.

@ceki
Copy link
Member

ceki commented Apr 17, 2024

qos-ch/slf4j#415 (comment) is closely related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants