77

In my Spring boot 2 project:

In build.gradle:

dependencies {
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'com.h2database:h2'
    implementation 'javax.servlet:jstl:1.2'
    implementation 'org.springframework.boot:spring-boot-devtools'
    implementation('org.springframework.boot:spring-boot-starter') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation "org.springframework.boot:spring-boot-starter-web"

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}

In src/resources/log4j2.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="Console"
              class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <!-- l, L, M - is extremely slow. It's use should be avoided unless execution
                speed is not an issue. -->
            <param name="ConversionPattern"
                   value="%p: %d{dd.MM.yyyy HH:mm:ss.SSS} %l %n    %m%n"/>
        </layout>
    </appender>

    <appender name="File"
              class="org.apache.log4j.DailyRollingFileAppender">
        <param name="Encoding" value="UTF-8"/>
        <param name="File" value="logs/trace.log"/>
        <param name="Append" value="true"/>
        <layout class="org.apache.log4j.PatternLayout">
            <!-- l, L, M - is extremely slow. It's use should be avoided unless execution
                speed is not an issue. -->
            <param name="ConversionPattern"
                   value="%p: %d{dd.MM.yyyy HH:mm:ss.SSS} %l %n    %m%n"/>
        </layout>
    </appender>

    <!-- Application Loggers -->
    <logger name="com.journaldev.spring">
        <level value="info"/>
    </logger>

    <!-- 3rdparty Loggers -->
    <logger name="org.springframework.core">
        <level value="info"/>
    </logger>

    <logger name="org.hibernate">
        <level value="info"/>
    </logger>

    <logger name="org.springframework.beans">
        <level value="info"/>
    </logger>

    <logger name="org.springframework.context">
        <level value="info"/>
    </logger>

    <logger name="org.springframework.web">
        <level value="info"/>
    </logger>

    <!-- The root category is used for all loggers unless a more specific logger
        matches. If none of the loggers are assigned a level, then all loggers inherit
        the level of the root logger which is set to DEBUG by default -->
    <root>
        <level value="ALL"/>
        <appender-ref ref="Console"/>
        <!-- <appender-ref ref="File" /> -->
    </root>

</log4j:configuration>

In my controller:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class CategoryController {
    private CategoryRepository categoryRepository;

    private static Logger logger = LoggerFactory.getLogger(CategoryController.class);

 @GetMapping("/categories")
    public String getCategories(Model model) {
        logger.debug("getCategories>>>>>>>>>>>>>>>>");
        model.addAttribute("categoryList", this.categoryRepository.findAll());
        return "categories/category_list";
    }
}

But when I start project I get error:

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

veben
  • 19,637
  • 14
  • 60
  • 80
Alexei
  • 14,350
  • 37
  • 121
  • 240
  • 1
    Please read the documentation https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging – Simon Martinelli Jan 07 '20 at 13:22
  • 1
    Although it isn't causing this error, the configuration file being used is for Log4j 1.x not Log4j 2. – rgoers Jan 08 '20 at 02:43
  • With Spring boot `2.3.0.RELEASE`+ version, support Log4j2 natively. See: https://stackoverflow.com/a/61873064/8718377 – veben Aug 12 '21 at 11:44

6 Answers6

102

According to Spring documentation (as pointed out by Simon), we want to exclude the "spring-boot-starter-logging" module from all libraries, not just from "spring-boot-starter-web".

configurations {
    ...
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

...instead of...

dependencies {
    ...
    implementation('org.springframework.boot:spring-boot-starter') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

I myself had the same problem and solved it with this solution.

kellyfj
  • 6,586
  • 12
  • 45
  • 66
nfvp
  • 1,146
  • 1
  • 8
  • 6
  • 1
    I am getting an warning from Intellij that exclude cannot be applied to 'java.lang.string' – akash bondre Feb 22 '21 at 09:11
  • I think that was just warning and it has worked fine after gradle build, thanks. – akash bondre Feb 25 '21 at 09:16
  • 1
    in case your using kotlin dsl like me: ```configurations { all { exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging") } }``` – jkerak Dec 29 '21 at 20:43
  • 2
    How do I do the above (exclude from all libraries) if I am using Maven instead of Gradle? Can you give the Maven equivalent of the above please? – Ezani Dec 04 '22 at 15:36
43

Spring boot 2.3.0.RELEASE version, support Log4j2 natively, for logging configuration if it is on the classpath. In this case, you can simply remove other log4j dependencies.

In other case, if you use the starters for assembling dependencies, you have to exclude Logback and then include log4j 2 instead:

You can do like that with Gradle:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

Or with Maven:

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

More information on the official documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging

veben
  • 19,637
  • 14
  • 60
  • 80
4

From the error logs decide which project to exclude.

e.g for a error msg like this: Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j I've used the gradle exclude:

configurations.all { exclude group: 'org.apache.logging.log4j'

}

justCurious
  • 720
  • 7
  • 13
2

I excluded spring boot logging from build.gradle but issue was still occurring. It got resolved by removing org.apache.logging.log4j/log4j-slf4j-impl/2.12.1 from .classpath

Prashant S
  • 349
  • 4
  • 14
2

The best way to solve this would be to run gradle dependencies, and identify where the log4j-to-slf4j is coming from, and then exclude this module in build.gradle

Gayathri
  • 95
  • 1
  • 1
  • 11
0

Even after correcting classpath, I had the same problem in STS and Gradle 'Refresh All' solved it.

Prashant S
  • 349
  • 4
  • 14