헤르메스 LIFE

[Exception] SLF4J: Class path contains multiple SLF4J bindings 본문

Exception

[Exception] SLF4J: Class path contains multiple SLF4J bindings

헤르메스의날개 2021. 1. 9. 14:31
728x90

개발환경

Spring Boot 2.2.4

JDK 1.8.0_202

REST + log4j2 설정 시 아래와 같은 오류가 발생했습니다.

SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/hermeswing/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/hermeswing/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] DEBUG StatusLogger Using ShutdownCallbackRegistry class org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry DEBUG StatusLogger org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good! WARN StatusLogger Multiple logging implementations found: Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10 Factory: org.apache.logging.slf4j.SLF4JLoggerContextFactory, Weighting: 15 Using factory: org.apache.logging.slf4j.SLF4JLoggerContextFactory Logging system failed to initialize using configuration from 'classpath:log4j2.xml' java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:17 - no applicable action for [Properties], current ElementPath is [[Configuration][Properties]]
DEBUG StatusLogger org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good! DEBUG StatusLogger Using ShutdownCallbackRegistry class org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry WARN StatusLogger Multiple logging implementations found: Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10 Factory: org.apache.logging.slf4j.SLF4JLoggerContextFactory, Weighting: 15 Using factory: org.apache.logging.slf4j.SLF4JLoggerContextFactory Exception in thread "main" java.lang.ExceptionInInitializerError at com.sample.Application.main(Application.java:17) Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j at org.apache.logging.slf4j.Log4jLoggerFactory.validateContext(Log4jLoggerFactory.java:49) at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:39) at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:30) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:54) at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363) at org.apache.commons.logging.LogAdapter$Slf4jAdapter.createLocationAwareLog(LogAdapter.java:130) at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:91) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59) at org.springframework.boot.SpringApplication.(SpringApplication.java:196) ... 1 more

pom.xml 파일에서 아래와 같이 추가만하면 된다고 합니다.... 만..

        <!-- Log4j2 로깅 구현체를 사용을 위해 Spring Boot Starter에서 지원해주는  log4j2 의존성을 추가 해준다. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

Spring Boot 는 기본적으로 logback 를 포함하고 있다고 합니다.

spring-boot-starter-logging 을 제외해 줘야 합니다.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringBootSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--
                 스프링 부트에서 Log4j2를 사용하기위해선 내부로깅에서 쓰이는 의존성을 제외해주어야 합니다.
                 기본적으로 Spring은 Slf4j라는 로깅 프레임워크를 사용합니다.
                 구현체를 손쉽게 교체할 수 있도록 도와주는 프레임 워크입니다.
                 Slf4j는 인터페이스고 내부 구현체로 logback을 가지고 있는데,
                 Log4j2를 사용하기 위해 exclude 해야 합니다.
                 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Log4j2 로깅 구현체를 사용을 위해 Spring Boot Starter에서 지원해주는  log4j2 의존성을 추가 해준다. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <!--  빌드 플러그인 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

gradle 를 사용하는 경우 참조 huisam.tistory.com/entry/log4j2

 

Spring Boot 에서 Log4j2 를 설정하는 방법

로그를 남기는 방법? 우리가 왜 Application에 log를 남기는 것이 중요한지 에 대해서는 지난시간에 살펴보았죠 ㅎㅎ 기억이 안나신다면 이전 포스팅을 참고해주세요 :) huisam.tistory.com/entry/springlogging

huisam.tistory.com

 

참조 : hermeslog.tistory.com/448?category=302344

 

[Spring Boot] SLF4J를 이용한 Log 설정 시 주의점

출처 : 백기선님의 강의 www.youtube.com/watch?v=o2-JaRD9qQE&t=907s&ab_channel=springcamp.io 1. 중요 !!! - SLF4J ( Simple Logging Facade for Java ) 는 Interface 입니다. - Spring Boot 은 기본적으로 JC..

hermeslog.tistory.com

참조 : hermeslog.tistory.com/449

 

[IntelliJ] Spring Boot Sample REST + Log4J2

개발환경 Spring Boot 2.2.4 JDK 1.8.0_202 REST Postman : REST Client 목표 1. Spring Boot REST 환경 2. Log4j2 추가 기존 프로젝트( https://hermeslog.tistory.com/445?category=530345 )에 Logging 설정을..

hermeslog.tistory.com

 

728x90