Contents

logback ์„ค์ •ํ•˜๊ธฐ

์ž๋ฐ” ๊ฐœ๋ฐœ์ž๋ผ๋ฉด ํ•œ๋ฒˆ์ฏค์€ ๋“ค์–ด๋ดค๊ณ , ํ•œ๋ฒˆ์ฏค์€ ์‚ฌ์šฉํ–ˆ์„๋ฒ•ํ•œ logger ๋กœ log4j๊ฐ€ ์žˆ์„๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ตœ๊ทผ๋“ค์–ด logback์ด๋ผ๋Š”๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๊ณ , ์™œ logback์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋ผ๋Š” ๊ธ€์ด ์žˆ์„์ •๋„๋กœ ์—ฌ๋Ÿฌ ์ธก๋ฉด์—์„œ ๊ฐœ์„ ์ด ๋œ๋“ฏ ํ•˜๋‹ค. (๋งํฌ) ์ด๋ฒˆ์— ์ž‘์„ฑํ•  ๊ธ€์˜ ๋ชฉ์ ์€ logback์„ ์„ค์ •ํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์ž‘์„ฑํ•ด ๋ณด๊ณ ์ž ํ•œ๋‹ค. โ€ป ๊ณต์‹์‚ฌ์ดํŠธ : https://logback.qos.ch/

pom.xml

maven๊ตฌ์กฐ๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„๋•Œ logback Dependency๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด pom.xml ์— ์„ค์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.7</version> <!--๋ฒ„์ „์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ -->
</dependency>

๋กœ๊ทธ๋ ˆ๋ฒจ

ERROR, WARN, INFO, DEBUG or TRACE

# logback ์„ค์ •ํŒŒ์ผ

์ผ๋ฐ˜์ ์œผ๋กœ logback.xml ์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ค์–ด src/main/resources/์•„๋ž˜์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค. Spring-Boot ํ™˜๊ฒฝ์—์„œ๋Š” logback-spring.xml ์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ logback.xml๋กœ ์„ค์ •ํ•˜๋ฉด ์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์„ค์ •ํ•˜๊ธฐ ์ „์— ๋กœ๊ทธ๋ฐฑ ๊ด€๋ จํ•œ ์„ค์ •์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ์–ดํ•  ์ˆ˜๊ฐ€ ์—†๊ฒŒ ๋œ๋‹ค. ( ๊ณต์‹์‚ฌ์ดํŠธ ๋ฉ”๋‰ด์–ผ : https://logback.qos.ch/documentation.html )

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />    

    <!-- ๋ณ€์ˆ˜ ์ง€์ • -->
    <property name="LOG_DIR" value="/logs" />
    <property name="LOG_PATH_NAME" value="${LOG_DIR}/data.log" />

    <!-- FILE Appender -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH_NAME}</file>
        <!-- ์ผ์ž๋ณ„๋กœ ๋กœ๊ทธํŒŒ์ผ ์ ์šฉํ•˜๊ธฐ -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH_NAME}.%d{yyyyMMdd}</fileNamePattern>
            <maxHistory>60</maxHistory> <!-- ์ผ์ž๋ณ„ ๋ฐฑ์—…ํŒŒ์ผ์˜ ๋ณด๊ด€๊ธฐ๊ฐ„ -->
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%F]%M\(%L\) : %m%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%F]%M\(%L\) : %m%n</pattern>
      </layout>
    </appender>

    <!-- TRACE > DEBUG > INFO > WARN > ERROR, ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์•ˆํ•จ -->
    <!-- profile ์„ ์ฝ์–ด์„œ appender ์„ ์„ค์ •ํ• ์ˆ˜ ์žˆ๋‹ค.(phase๋ณ„ ํŒŒ์ผ์„ ์•ˆ๋งŒ๋“ค์–ด๋„ ๋˜๋Š” ์ข‹์€ ๊ธฐ๋Šฅ) -->
    <springProfile name="local">
      <root level="DEBUG">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
      </root>
    </springProfile>
    <springProfile name="real">
      <root level="INFO">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
      </root>
    </springProfile>
</configuration>

java ์ฝ”๋”ฉ์—์„œ์˜ ๋กœ๊น…

์‹ค์ œ ์‚ฌ์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด LoggerFactory๋ฅผ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ Lombok์–ด๋…ธํ…Œ์ด์…˜์„ ํ™œ์šฉํ•˜๋ฉด ์‹ฌํ”Œํ•˜๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • LoggerFactory ์‚ฌ์šฉ
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foo {
    static final Logger logger = LoggerFactory.getLogger(Foo.class);

    public void test() {
        logger.debug("ID : {}", "foo");
    }
}
  • Lombok ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Foo {

    public void test() {
        log.debug("ID : {}", "foo");
    }
}

๋งˆ์น˜๋ฉฐ

์ผ๋ฐ˜์ ์ธ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” WAS์—์„œ ๋กœ๊น…์„ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— file ๋กœ ๋กœ๊น…์„ ํ•  ํ•„์š”๋Š” ์—†์„๊ฒƒ ๊ฐ™๋‹ค.(์ผ๋ฐ˜ jar ํ˜•ํƒœ์—์„œ๋Š” ํŒŒ์ผ ๋กœ๊น…์ด ํ•„์š” ํ• ์ˆ˜๋„…)

์ฐธ๊ณ ์‚ฌ์ดํŠธ


Buy me a coffeeBuy me a coffee