Dependencies

Enabling the Spring Boot application to use Log4j2

<!-- Exclude Spring Boot's Default Logging -->
<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>

<!-- Add Log4j2 Dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Log4j Configuration

Add to classpath (or into src/main/resources folder) one of the following files:

  • log4j2-spring.xml
  • log4j2.xml
  • log4j2.properties

See Log4j2 for more configurations

Example log4j-spring.xml file

Spring Boot Log4j2 Demo - Using Slf4j

Spring Boot Log4j2 Demo - Using Log4j2 Directly