로그 남기기
문제가 생기면 로그를 보고 수정하기 위함
프로퍼티 파일에서 설정을 추가한다
server.port=8080
# vscode console highlight
spring.output.ansi.enabled=always
# utf-8
server.servlet.encoding.charset=utf-8
server.servlet.encoding.force=true
# DB
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
# JPA table create or none
spring.jpa.hibernate.ddl-auto=create
# query log
spring.jpa.show-sql=true
# dummy data
spring.sql.init.data-locations=classpath:db/data.sql
# create dummy data after ddl-auto create
spring.jpa.defer-datasource-initialization=true
# mustache request expose
###spring.mustache.servlet.expose-request-attributes=true
###spring.mustache.servlet.expose-session-attributes=true
# sql formatter
spring.jpa.properties.hibernate.format_sql=true
# log
logging.level.shop.mtcoding.blog=DEBUG
logging.level.org.hibernate.type=TRACE
logging.level.shop.mtcoding.blog=DEBUG
→ 앱에서 로그 레벨을 디버깅 수준으로 만든다
logging.level.org.hibernate.type=TRACE
→ db에서 로그 레벨을 트레이스 수준으로 만든다
📋 SLF4J 로깅 수준 (낮은 -> 높은 순)
레벨 | 설명 | 예시 |
trace | 가장 상세한 로그. 디버깅보다 더 세밀한 정보 | log.trace("Trace message") |
debug | 개발 시 디버깅용 로그. 서비스 시에는 주로 끔 | log.debug("Debug message") |
info | 일반적인 정보 로그. 운영 환경에서도 출력 가능 | log.info("Info message") |
warn | 경고 메시지. 문제가 발생할 가능성이 있음 | log.warn("Warning message") |
error | 오류 메시지. 예외 발생 등 치명적인 문제 | log.error("Error message", e) |
🔧 사용 예시
@Slf4j
public class MyService {
public void doSomething() {
log.trace("이건 trace입니다.");
log.debug("디버깅용 메시지");
log.info("일반 정보 출력");
log.warn("경고 발생!");
log.error("에러 발생!", new RuntimeException("예외"));
}
}
⚠️ 실제 로그가 출력되려면 logback.xml, log4j2.xml 등 설정 파일에서 로깅 수준을 적절히 설정해야 합니다. 예: root level="info"로 설정하면 info, warn, error만 출력되고 debug, trace는 무시됩니다.

더 이상 sysout 을 사용하지 않아도 된다
Share article