ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

โœ… Spring MDC๋ฅผ ์‚ฌ์šฉํ•ด ์‹๋ณ„๊ฐ€๋Šฅํ•œ ๋กœ๊ทธ ๋‚จ๊ธฐ๊ธฐ

์šด์˜์„ ํ•˜๋‹ค ๋ณด๋ฉด ๋กœ๊ทธ๋ฅผ ์ถ”์ ํ•ด์„œ ํ™•์ธํ•ด์•ผ ํ• ๋•Œ๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ ์—ฌ๋Ÿฌ ์š”์ฒญ์ด ํ•œ๊บผ๋ฒˆ์— ๋“ค์–ด์˜ค๋ฉด ๋กœ๊ทธ๊ฐ€ ์ˆœ์„œ ์—†์ด ์Œ“์—ฌ ํ™•์ธํ•˜๊ธฐ ์–ด๋ ค์šธ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด๋•Œ ์š”์ฒญ๋‹น ์œ ๋‹ˆํฌํ•œ ๊ฐ’์„ ์ฃผ์–ด ๋กœ๊ทธ์— ํ•จ๊ป˜ ์ถœ๋ ฅํ•จ์œผ๋กœ์จ ์ถ”์ ํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

MDC (Mapped Diagnostic Context)

- ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œํ™˜๊ฒฝ์—์„œ ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์“ฐ๋ ˆ๋“œ์— ๋ฉ”ํƒ€ ์ •๋ณด๋ฅผ ๋„ฃ๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ Map์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๊ณ  ThreadLocal์— ํ‚ค ๊ฐ’์„ ์ €์žฅํ•˜์—ฌ Thread๊ฐ€ ์กด์žฌํ•˜๋Š” ๋™์•ˆ ๊ณ„์† ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

 

โœ… MDC ํ•„ํ„ฐ์— ๊ตฌํ˜„ํ•˜๊ธฐ

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MDCLoggingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        MDC.put("uuid", UUID.randomUUID().toString().replaceAll("-", ""));
        chain.doFilter(request, response);
        MDC.clear();
    }
}

MDC๋ฅผ ๋„ฃ์–ด์ค„๋งŒํ•œ๊ณณ์€ ํ•„ํ„ฐ๋‚˜ ์ธํ„ฐ์…‰ํ„ฐ๋“ฑ์ด ์žˆ๋Š”๋ฐ ๊ฐ€์žฅ ์•ž๋‹จ์ธ ํ•„ํ„ฐ์— ์ ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹๊ณ  ํ•„ํ„ฐ์ค‘์—๋„ ๊ฐ€์žฅ ์•ž๋‹จ์— ๋จผ์ € ๋“ฑ๋กํ•ด์ค€๋‹ค.

Spring MVC๋Š” ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋‘์—ˆ๋‹ค๊ฐ€ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ๋งŒ๋“ค์–ด๋‘” ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ ์ฒ˜๋ฆฌํ›„ ๋ฐ˜๋‚ฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚จ์•„ ์žˆ์„์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ MDC.clear์–ด ํ•ด์ค๋‹ˆ๋‹ค.

 

logback ํŒจํ„ด

// Logback
%X{<ํ‚ค๊ฐ’>:-<Default>}]
[%X{uuid:-startup}] %d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %-40.40logger{39} [%4L] : %m%n

๋‹จ์ผ์„œ๋ฒ„๋‚ด์—์„œ ์‚ฌ์šฉ์„ ํ•˜๊ณ  ๋ถ„์‚ฐํ™˜๊ฒฝ์ผ ๊ฒฝ์šฐ spring-cloud-sleuth๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”

 

์ฐธ๊ณ 

- https://mangkyu.tistory.com/266

- https://055055.tistory.com/96

๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ
๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (80)
๐Ÿ“บ Develop (0)
๐ŸŒ‹ Error Fixed (5)
๐Ÿ— Tool (5)
๐Ÿ’ป MacBook M1 (15)
๐Ÿ“ฆ ETC (1)

์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค.