์ตœ๋Œ€ 1 ๋ถ„ ์†Œ์š”

๐Ÿ“˜Logger์— ๋Œ€ํ•ด์„œโ€ฆ

๋ชจ๋“  ํ–‰์œ„์™€ ์ด๋ฒคํŠธ ์ •๋ณด๋ฅผ ์‹œ๊ฐ„์˜ ๊ฒฝ๊ณผ์— ๋”ฐ๋ผ ๊ธฐ๋กํ•œ ๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ ์ƒ์—์„œ ๋กœ๊ทธ ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์„ ๋กœ๊น…(Logging) ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๋กœ๊น…(logging)์˜ ์žฅ์ 
๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๋žจ์˜ ๋””๋ฒ„๊น… ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ๋ฌธ์ œ์˜ ์›์ธ ํŒŒ์•… ์‹œ์Šคํ…œ ๋ฐ ์‚ฌ์šฉ์ž์˜ ๋™์ž‘ ํŒจํ„ด ๋ถ„์„ ํ•ดํ‚น(์นจ์ž…)์˜ ๋น„์ •์ƒ ๋™์ž‘์˜ ๊ธฐ๋ก์„ ๊ฐ์ง€ ๋ถ„์„์„ ํ†ตํ•œ ํ†ต๊ณ„ํ™”

1. Logger ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Logback, Log4J ๋“ฑ์ด ์žˆ์ง€๋งŒ ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ์ด ๊ฒƒ๋“ค์„ ํ†ตํ•ฉํ•ด์„œ ์“ฐ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ SLF4J์ด๋‹ค.

2. Log ์„ ์–ธ

[1๋ฒˆ ์˜ˆ์ œ]

// 1๋ฒˆ
private final Logger log = LoggerFactory.getLogger(getClass());
// 2๋ฒˆ
private static final Logger log = LoggerFactory.getLogger(Xxx.class)
// 3๋ฒˆ
@Slf4j
public class LogTestController {
}


[2๋ฒˆ ์˜ˆ์ œ]

    public TraceStatus begin(String message) {
        TraceId traceId = new TraceId();
        Long startTimeMs = System.currentTimeMillis();
        log.info("[{}] {}{}", traceId.getId(), addSpace(START_PREFIX, traceId.getLevel()), message);    // ๋กœ๊ทธ ํ˜ธ์ถœ

        // ๋กœ๊ทธ ์ถœ๋ ฅ
        return new TraceStatus(traceId, startTimeMs, message);
    }

3. ๋กœ๊ทธ ๋ ˆ๋ฒจ

๋กœ๊ทธ์—๋Š” ๋ ˆ๋ฒจ์ด ์žˆ๋‹ค!

trace -> debug -> info -> warn -> error   
  • ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐœ๋ฐœ ์„œ๋ฒ„๋Š” debug, ์šด์˜ ์„œ๋ฒ„๋Š” info๋กœ ์„ค์ •ํ•จ.

[๋กœ๊ทธ ํ˜ธ์ถœ]

@Slf4j
@RestController
public class LogTestController {

    @RequestMapping("/log-test")
    public String logTest(){
        // ๋กœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉํ•œ ์ถœ๋ ฅ
        log.trace("trace log={}", name);
        log.debug("debug log={}", name);
        log.info("info log={}", name);
        log.warn("warn log={}", name);
        log.error("error log={}", name);

        return "ok";
    }
}

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ