Spring boot 를 만들때 의존성을 주입 받았을 것다.
의존성 주입을 위해 maven 이나 gradle 을 썼을 것이고
maven 의 경우 pom.xml
gradle 의 경우 build.gradle 을 열어서 단어 찾기를 했을때
파일내에 'org.springframework.boot:spring-boot-starter' 란 내용이 들어가 있으면
이미 log 를 쓸수 있는 환경인 것이다.
Spring Boot에서는 org.slf4j.Logger와 org.slf4j.LoggerFactory를 사용해 로깅을 처리한다.
java 파일 안에서 member 변수로 Logger 객체를 만들고 쓰면 된다.
( LoggerFactory 로 -> ExampleLoggr.class 의 Logger를 만든 예)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class ExampleLogger {
private static final Logger logger = LoggerFactory.getLogger(ExampleLogger.class);
public void logMessages() {
logger.debug("This is a DEBUG message");
logger.info("This is an INFO message");
logger.warn("This is a WARN message");
logger.error("This is an ERROR message");
}
}
log 의 레벌은 application.properties 혹은 application.ym 에서 하면 된다.
logging.level.root=INFO
logging.level.org.springframework=DEBUG
logging.level.com.example=TRACE
하지만 내가 본건 저렇게 쓰지 않았다.
아래와 같은 소스 코딩이 존재 없이 그냥 쓰고 있었다. ( 대신 파일 위에 @Slf4j <- 이게 포함되어 있었다)
private static final Logger logger = LoggerFactory.getLogger(ExampleLogger.class);
==내가 본 소스의 예==
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ExampleService {
public void logMessages() {
log.debug("This is a DEBUG message");
log.info("This is an INFO message");
log.warn("This is a WARN message");
log.error("This is an ERROR message");
}
}
위쪽 소스와 비교해 Logger 객체를 선언이 없다.
log.debug() 라고 쓰는데 log 는 어디에도 선언이 되어 있지 않다.
이게 어떻게 가능 하지???
이유는
"@Slf4j" 라는 annotation 때문이다. 이건 Lombok 이란 libary 가 제공 하는 annotation으로
해당 annotaion을 파일의 상단에 붙이면
알아서 자동으로 Logger 객체를 생성 하고 그 객체의 이름은 log 로 파일에 주입 한다.
그래서 생성이 없이 log 를 쓸수 있는 것이다.
또 드는 질문?
@slf4j 를 쓰면 다른 설정이 필요 없나??
천만의 말씀..
답은 아래 접는 페이지에.
@Slf4j는 단순히 SLF4J의 로거 객체를 생성하는 데 도움을 주는 Lombok의 어노테이션입니다.
따라서 로그 레벨 설정은 반드시 별도로 필요합니다. @Slf4j를 사용한다고 해서 로그 레벨을 설정하지 않아도 되는 것은 아닙니다.
기본 동작
- 로그 레벨 설정을 하지 않은 경우:
- Spring Boot는 기본적으로 INFO 레벨을 사용합니다.
- 따라서 DEBUG나 TRACE 레벨의 로그는 출력되지 않습니다.
- 로그 레벨 설정이 필요한 이유:
- 애플리케이션의 디버깅 및 문제 해결을 위해 특정 레벨의 로그만 출력하거나 제한할 필요가 있습니다.
- 로그 출력은 application.properties, application.yml, 또는 Logback 설정 파일(logback-spring.xml)을 통해 조정합니다.
까탈스러운 고객이 log 를 보면서 운영할때 보기 편하도록 log 의 형식을 바꿔달라고 한다면??
==로그 출력 형식 변경법==
SLF4J의 기본 구현체인 Logback의 설정 파일을 수정해 로그 출력 형식을 변경할 수 있다.
logback-spring.xml
src/main/resources에 logback-spring.xml 파일을 생성 한다.
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
오늘도 하나 배운것에 보람을 느끼며.
읽어 주셔서 감사.

'SI 업무 > spring boot' 카테고리의 다른 글
| @Configuration 의 용도? Interface 를 어떻게 주입하지? (0) | 2024.12.26 |
|---|---|
| 5. JWT, Spring Security (0) | 2024.12.02 |
| 4. profile 로 다른 설정 관리 (0) | 2024.11.29 |
| 2. 복수개의 data source 연결은 어떻게? (4) | 2024.11.27 |
| 1. 한개의 data source 쓸려면 어떻게 할까? (1) | 2024.11.27 |