๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

[Spring] logging ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

by dkswnkk 2022. 3. 21.

์„œ๋ก 

์‹ค๋ฌด์—์„œ๋Š” System.out.println()๊ณผ ๊ฐ™์€ ์‹œ์Šคํ…œ ์ฝ˜์†”์„ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ๋””๋ฒ„๊น…ํ•˜์ง€ ์•Š๊ณ , ๋ณ„๋„์˜ logging ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋””๋ฒ„๊น…์ด๋‚˜ ํƒ€์ž„์Šคํƒฌํ”„ ๋“ฑ ์ •ํ•ด์ง„ ์–‘์‹์— ๋งž์ถ”์–ด ํ™”๋ฉด ์ƒ์ด๋‚˜ ํŒŒ์ผ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธธ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์€ ๋ชฉ์ฐจ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ชฉ์ฐจ
1. logging์˜ ๋Œ€ํ‘œ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค.
2. Spring Boot์—์„œ logging์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค.
3. logging๊ณผ System.out.println()์„ ๋น„๊ตํ•ด ๋ณธ๋‹ค.

 

1. logging์˜ ์ข…๋ฅ˜

logging ๊ด€๋ จ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ log4j, logback, log4j2, ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์„ ํ†ตํ•ฉํ•ด์„œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ œ๊ณตํ•˜๋Š” SLF4J ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. log4j -> logback -> log4j2 ์ˆœ์„œ๋กœ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฉฐ, logback๊ณผ log4j๋Š” ๋‘˜ ๋‹ค log4j๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„ค์ •์ด๋‚˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์ด ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. log4j๋Š” 2015๋…„ 8์›” 5์ผ ์ดํ›„๋กœ ์ง€์›์ด ์ข…๋ฃŒํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ œ์™ธํ•˜๊ณ  ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

logback 1. log4j์ดํ›„์— ์ถœ์‹œ๋œ ๋ณด๋‹ค ํ–ฅ์ƒ๋˜๊ณ  ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” Java logging ํ”„๋ ˆ์ž„ ์›Œํฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
2. slf4j์˜ ๊ตฌํ˜„์ฒด๋กœ์จ ๋™์ž‘ํ•˜๊ธฐ์— Spring Bootํ™˜๊ฒฝ์—์„œ๋Š” spring-boot-starter-web์•ˆ์— spring-boot-starter-logging์˜ logback์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋˜์–ด ์žˆ์–ด์„œ ๋ณ„๋‹ค๋ฅธ dependency ์ถ”๊ฐ€ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
log4j2 1. ๊ฐ€์žฅ ์ตœ์‹ ์— ๋‚˜์˜จ logging ํ”„๋ ˆ์ž„์›Œํฌ๋กœ์จ Apache log4j์˜ ๋‹ค์Œ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.
2. logback์ฒ˜๋Ÿผ ํ•„ํ„ฐ๋ง ๊ธฐ๋Šฅ๊ณผ ์ž๋™ ๋ฆฌ๋กœ๋”ฉ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
3. logback๊ณผ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” Multi Thread ํ™˜๊ฒฝ์—์„œ ๋น„๋™๊ธฐ ๋กœ๊ฑฐ(Async Logger)์˜ ๊ฒฝ์šฐ ๋‹ค๋ฅธ logging ํ”„๋ ˆ์ž„์›Œํฌ๋ณด๋‹ค ์ฒ˜๋ฆฌ๋Ÿ‰์ด ํ›จ์”ฌ ๋งŽ๊ณ , ๋Œ€๊ธฐ์‹œ๊ฐ„์ด ํ›จ์”ฌ ์งง์Šต๋‹ˆ๋‹ค.
4. Java8๋ถ€ํ„ฐ ๋„์ž…๋œ ๋žŒ๋‹ค์‹์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ์ด๋ฏธ์ง€๋Š” Multi Thread ํ™˜๊ฒฝ์—์„œ logging ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์„ฑ๋Šฅ์„ ๋น„๊ตํ•œ ์ฐจํŠธ์ž…๋‹ˆ๋‹ค.

https://logging.apache.org/log4j/2.x/performance.html

 

2. Spring Boot์—์„œ์˜ logging ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

๊ทธ๋ ‡๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์–ด๋–ป๊ฒŒ logging์„ ์‚ฌ์šฉํ•˜๊ณ , ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

package springboot.MVCBasic.basic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LogTestController {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @GetMapping("log-test")
    public String logTest() {

        String name = "Spring";
        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";
    }

}

๋ณด์‹œ๋Š” ๋ฐ”์™€ ๊ฐ™์ด log๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์„ ์–ธ์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ log์˜ ์ข…๋ฅ˜์—๋Š” trace, debug, info, warn, error์ด ์žˆ๋Š”๋ฐ ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋Š” ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. (๋ณดํ†ต ๊ฐœ๋ฐœ ์„œ๋ฒ„๋Š” debug, ์šด์˜ ์„œ๋ฒ„๋Š” info๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.)

Level Color Mean
1. error Red ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ค‘ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ
2. warn Yellow ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ์ด์ง€๋งŒ, ํ–ฅํ›„ ์‹œ์Šคํ…œ ์—๋Ÿฌ์˜ ์›์ธ์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ
3. info Green ๋กœ๊ทธ์ธ์ด๋‚˜ ์ƒํƒœ ๋ณ€๊ฒฝ๊ณผ ๊ฐ™์€ ์ •๋ณด์„ฑ ๋ฉ”์‹œ์ง€
4. debug Green ๊ฐœ๋ฐœ์‹œ ๋””๋ฒ„๊น… ๋ชฉ์ ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์‹œ์ง€
5. trace Green debug ๋ณด๋‹ค ์ข€ ๋” ์ƒ์„ธํ•œ ๋ฉ”์„ธ์ง€

ํŠน์ • ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์ง€์ •ํ•˜๋ฉด ํ•ด๋‹น ๋กœ๊ทธ ๋ ˆ๋ฒจ์˜ ์ƒ์œ„ ์šฐ์„ ์ˆœ์œ„ ๋กœ๊ทธ๊ฐ€ ๋ชจ๋‘ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŠน์ • ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ info๋กœ ์ง€์ •ํ•˜๋ฉด info, warn, error ๋กœ๊ทธ๊ฐ€ ์ „๋ถ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
์ฆ‰ ํŠน์ • ๋กœ๊ทธ ๋ ˆ๋ฒจ์ด info๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

2022-03-21 17:38:15.537 INFO 31713 --- [nio-8080-exec-1] LogService
2022-03-21 17:38:15.537 ERROR 31713 --- [nio-8080-exec-1] LogService
2022-03-21 17:38:15.537 WARN 31713 --- [nio-8080-exec-1] LogService

๋กœ๊ทธ ๋ ˆ๋ฒจ์˜ default ๊ฐ’์€ info์ด๋ฉฐ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
1. application.yml์—์„œ ์„ค์ •ํ•  ๋•Œ

# ํŒจํ‚ค์ง€์™€ ๊ทธ ํ•˜์œ„ ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ • (๋””ํดํŠธ ๊ฐ’์€ info)
logging:
  level:
    springboot.MVCBasic: debug 		#springboot.MVCBasic ํŒจํ‚ค์ง€์˜ ํ•˜์œ„ ๋ ˆ๋ฒจ ์„ค์ •

2. application.properties์—์„œ ์„ค์ •ํ•  ๋•Œ

#์ „์ฒด ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ •(๊ธฐ๋ณธ info)
logging.level.root=info

#hello.springmvc ํŒจํ‚ค์ง€์™€ ๊ทธ ํ•˜์œ„ ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ •
logging.level.hello.springmvc=debug


์šฐ๋ฆฌ๋Š” ์œ„ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ private final Logger log = LoggerFactory.getLogger(getClass()); ๋ฅผ ์„ ์–ธํ•ด์ค€ ๊ฒƒ์„ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค lombok์˜ @slf4j ์• ๋…ธํ…Œ์ด์…˜๋งŒ ์„ ์–ธํ•ด์ค€๋‹ค๋ฉด ์•„๋ž˜์™€ ๋ณ„๋„๋กœ ์„ ์–ธํ•ด ์ฃผ์ง€ ์•Š๊ณ ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

package springboot.MVCBasic.basic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class LogTestController {

    @GetMapping("log-test")
    public String logTest() {

        String name = "Spring";
        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";
    }

}

์—ฌ๊ธฐ์„œ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค ๋ณด๋ฉด ๋ˆ„๊ตฐ๊ฐ€๋Š” log.trace("trace log = {}", name);์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  log.trace("trace log" + name); ๋กœ ํ•˜๋ฉด ์•ˆ ๋˜๋‚˜?๋ผ๊ณ  ์˜๋ฌธ์ด ๋“œ์‹ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ๋จผ์ € ๊ฒฐ๋ก ์ ์œผ๋กœ ๋ง์”€๋“œ๋ฆฌ์ž๋ฉด ํ›„์ž์˜ ๋ฐฉ์‹์€ ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๊ทธ ์ด์œ ๋Š” ํ›„์ž์˜ ๋ฐฉ์‹์€ logging level์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์•„์„œ ์ถœ๋ ฅํ•  ํ•„์š”๊ฐ€ ์—†์Œ์—๋„ ์ผ๋‹จ ๋ฌธ์ž์—ด์„ ์„œ๋กœ ๋”ํ•˜๋Š” ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„์šฉ๋ฉด์—์„œ ์†ํ•ด๋ฅผ ๋ณด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ „์ž์˜ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒํ•ฉ๋‹ˆ๋‹ค.

3. System.out.println()์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  logging์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ๊ตณ์ด println๋ฌธ์œผ๋กœ ์ฐ์œผ๋ฉด ํŽธํ•  ๊ฒƒ์„ ๊ตณ์ด logging์„ ์ด์šฉํ• ๊นŒ์š”? ๊ทธ ์ด์œ ๋Š” logging์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์žฅ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
logging์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์˜ ์žฅ์ 

  1. ์Šค๋ ˆ๋“œ ์ •๋ณด, ํด๋ž˜์Šค ์ด๋ฆ„ ๊ฐ™์€ ๋ถ€๊ฐ€ ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ๋ณผ ์ˆ˜ ์žˆ๊ณ , ์ถœ๋ ฅ ๋ชจ์–‘์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. ๋กœ๊ทธ ๋ ˆ๋ฒจ์— ๋”ฐ๋ผ ๊ฐœ๋ฐœ์„œ๋ฒ„์—์„œ๋Š” ๋ชจ๋“  ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์šด์˜์„œ๋ฒ„์—์„œ๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š” ๋“ฑ ๋กœ๊ทธ๋ฅผ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ๋‹ค.
  3. ์‹œ์Šคํ…œ ์•„์›ƒ ์ฝ˜์†”์—๋งŒ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŒŒ์ผ์ด๋‚˜ ๋„คํŠธ์›Œํฌ ๋“ฑ, ๋กœ๊ทธ๋ฅผ ๋ณ„๋„์˜ ์œ„์น˜์— ๋‚จ๊ธธ ์ˆ˜ ์žˆ๋‹ค.
  4. ํŠนํžˆ ํŒŒ์ผ๋กœ ๋‚จ๊ธธ ๋•Œ์—๋Š” ์ผ๋ณ„, ํŠน์ • ์šฉ๋Ÿ‰์— ๋”ฐ๋ผ ๋กœ๊ทธ๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
  5. println์„ ์ผ์„ ๋•Œ๋ณด๋‹ค ๋‚ด๋ถ€ ๋ฒ„ํผ๋ง, ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ๋“ฑ์˜ ํ™˜๊ฒฝ์—์„œ ํ›จ์”ฌ ์ข‹๋‹ค

 

์ฐธ๊ณ 

๋Œ“๊ธ€