logback 에서 spring framework log 가 출력되지 않을때
문제
스프링은 로그 출력을 위해 jakarta commons logging 라이브러리를 사용하고 있다.
그러므로 로그백의 logger 에 org.springframework 을 설정해도 스프링 로그(Bean create등)는 로그백의 appender 에 출력되지 않는다.
해결
pom.xml 에 다음다음 절차를 하나씩 해보고 결과 확인
pom.xml 의 스프링 설정을 다음과 같이 commons-logging 의존성을 제거한다.
<properties> <spring-framework.version>3.2.3.RELEASE</spring-framework.version> <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> </properties> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
logback 과 slf4j 설정
<!-- LogBack dependencies --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency>
logback.xml 생성
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="30 seconds"> <property name="LOG_HOME" value="logs" /> <property name="LOG_PATTERN" value="%logger{36} %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/test-web-app.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>${LOG_HOME}/test-web-app.log.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- keep 30 days' worth of history --> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> ${LOG_PATTERN} </pattern> </encoder> </appender> <logger name="org.springframework" level="DEBUG" > <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </logger> <!-- turn OFF all logging (children can override) --> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>
정상 동작 여부를 확인한다.
jUnit Test Case에서 스프링 로그가 나오지 않는거면 해당 테스트 케이스가 @RunWith(SpringJUnit4ClassRunner.class) annotion 이 있는지 확인
Ref
- http://www.codingpedia.org/ama/how-to-log-in-spring-with-slf4j-and-logback/
- http://stackoverflow.com/questions/20823409/spring-logging-threshold-not-being-set-by-logback
- http://stackoverflow.com/questions/16355312/logback-logging-not-working
- http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/overview.html 의 Using SLF4J 항목