SpringBoot

Springboot ECS logging structure

pepega 2025. 2. 26. 23:37

1. 적용 사유

2. Marker가 뭔가요

3. ECS 로깅이 뭔가요

4. 적용 방법

5. 결과

 

 

1. 적용 사유

datadog에 로그를 쌓기 위해 ecs 포맷을 사용해야 했습니다.

그 중 개인정보가 포함되어있는 로그 정보를 datadog에 보낼 수 없었습니다.

그래서 slf4j 혹은 log4j 로그에 Marker를 추가하여

개인정보 로그는 보내지 않도록 설정하기로 하였습니다.

 

2. Marker가 뭔가요

로그에 Object를 추가하여 태그를 지정할 수 있습니다.

예를 들어 특정 Marker가 있는 로그는 출력하지 않기.

 

3. ECS 로깅이 뭔가요

ECS는 로그 및 메트릭과 같은 Elasticsearch 혹은 datadog 에 이벤트 데이터를 저장할 때 사용할 공통 필드 집합입니다.

 

  • 이벤트 소스 : 이벤트 소스가 Elastic 제품인지, 타사 제품인지, 조직에서 구축한 맞춤형 애플리케이션인지 여부입니다.
  • 수집 아키텍처 : 이벤트의 수집 경로에 Beats processor, Logstash, Elasticsearch 수집 노드가 포함되는지, 이 중 전부가 포함되는지, 아니면 이 중 어느 것도 포함되지 않는지 여부입니다.
  • Consumers : API, Kibana queries, dashboards, apps 또는 기타 수단을 통해 소비되는 경우입니다.

 

4. 적용 방법

# application.yml
logging:
  structured:
    format:
      console: ecs

사전에 로깅 관련 종속성 및 위 코드가 추가되어야 합니다.

4-1. springboot >= 3.5.x

 

 

요것만 적용하면 끝 입니다. (springboot 3.5.x 기준)

관련 PR

https://github.com/spring-projects/spring-boot/pull/43768

 

아직 3.4.x는 지원되지 않고 있습니다. 곧 되겠죠?

 

4-2. springboot <= 3.4.x

 

 

3.4.x 버전에서는 저 기능이 지원하지 않습니다.

그래서 MDC로 로그를 추가합니다.

 

다른 방법이 있으면 그걸 사용해도 좋아보여요.

 

 

5. 결과

맨 뒤에 tags key를 확인하면 됩니다!

5-1. springboot >= 3.5.x

{"@timestamp":"2025-03-16T13:24:14.670443800Z","log.level":"INFO","process.pid":8980,"process.thread.name":"http-nio-8080-exec-1","log.logger":"com.ghghghko.blog.hello.controller.HelloController","message":"hello","ecs.version":"8.11","tags":["hello"]}

 

5-2. springboot <= 3.4.x

{"@timestamp":"2025-03-16T13:17:58.697687600Z","log.level":"INFO","process.pid":24648,"process.thread.name":"http-nio-8080-exec-1","log.logger":"com.ghghghko.blog.hello.controller.HelloController","message":"hello","tags":"PRIVACY","ecs.version":"8.11"}

 

가독성 최악

 

전체 코드는

https://github.com/GHGHGHKO/marker-example

 

GitHub - GHGHGHKO/marker-example

Contribute to GHGHGHKO/marker-example development by creating an account on GitHub.

github.com

 

 

 

 

출처

https://www.elastic.co/guide/en/ecs/8.17/ecs-reference.html