반응형

로깅? 로그 찍기

 

 

1. 로킹 퍼사드 vs 로깅

 

로깅 퍼사드

: Commons Logging, SLF4j

: 로거 api를 추상화해 놓은 인터페이스

: 로거를 바꿔 끼울 수 있게 해줌

 

로깅

: JUL, Log4j2, Logback

 

스프링 프레임워크는 Commons Logging, Logback을 사용함

 

 

 

2. Spring-JCL

스프링 5부터 추가된 기능

Commons Logging -> SLF4j 로 보내도록 함

- pom.xml 에 exclusion 안해도됨

 

 

 

3. 더 자세한 로그를 찍고 싶다면?

-> program argument 에 --debug 로 설정(일부 핵심라이브러리만 디버깅 모드로)

-> program argument 에 --trace 로 설정(전부 다 디버깅 모드로)

 

 

 

4. 디버깅 컬러로 찍기

spring.output.ansi.enabled=always

appliction.properties

 

 

5. 파일 출력

logging.path=logs

appliction.properties

 

 

6. 로그 레벨 조정

logging.level.com.example.springapplication=DEBUG

appliction.properties

 

-> 자신의 파일 경로를 설정해주기

 

@Component
public class SampleRunner implements ApplicationRunner {

    private Logger logger = LoggerFactory.getLogger(SampleRunner.class);

    @Autowired
    private String hello;

    @Autowired
    private NaeunProperties naeunProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("=====================");
        logger.info(hello);
        logger.info(naeunProperties.getName());
        logger.info(naeunProperties.getFullName());
        logger.info("=====================");
    }
}

SampleRunner.java

 

결과 로그

 

 

7. 커스텀 로그 설정 파일 이용하기

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
    <logger name="com.example.springapplication" level="DEBUG"/>
</configuration>

logback-spring.xml

 

-> logger name 에 경로 설정해주기

 

 

 

8. 로거를 log4j2 로 변경하기

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<version>2.3.4.RELEASE</version>
	<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>

pom.xml

반응형

+ Recent posts