priyanka
priyanka

Reputation: 472

Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j Issue

I am writing Junits to test my logs. I am using LogCaptor. I have attached my actual method and my testcases along with pom.xml.

This is my method where i have logs defined.

@Override
public ApplicationResponse createApplication (final String serviceId, final String appId) throws KMSException     {
    log.debug("Entering createApplication");
    try {
        final Application application =
            applicationOperations.createApplication(appId, serviceId);
        return applicationResponse(application);
    }
    catch (KMSCommonException exception) {
        throw new KMSInternalServerErrorException("Not able to create application", exception);
    }
}

This is my test method to test Loggers :

@Test
public void testCreateApplicationLogger () throws KMSException {
    String expectedInfoMessage = "Entering createApplication";

    LogCaptor logCaptor = LogCaptor.forClass(Test.class);
    adminAPIApplicationOpService.createApplication(TEST_SERVICE, TEST_APP + 1);
    assertTrue( logCaptor.getInfoLogs().contains(expectedInfoMessage));

}

Upvotes: 6

Views: 18443

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18929

It is a conflict. Run the command

mvn dependency:tree -Dverbose -Dincludes=log4j-to-slf4j

Check from where you import that dependency and exclude it.

Reason is log4j is an API. You must include in your project an implementation for that API to work. Both log4j-slf4j-impl and log4j-to-slf4j are implementations for log4j API. When you include multiple implementations conflicts arise.

Upvotes: 6

Related Questions