logback

์ƒ์„ฑ์ผ: 2022๋…„ 10์›” 31์ผ ์˜คํ›„ 4:39

์ผ๋ฐ˜์ ์ธ ์ถœ๋ ฅ

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>./logs/logback.log</file>
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n</pattern> <!-- ํ•ด๋‹น ํŒจํ„ด ๋„ค์ด๋ฐ์œผ๋กœ ํ˜„์žฌ ๋กœ๊ทธ๊ฐ€ ๊ธฐ๋ก๋จ -->
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>./was-logs/info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <!-- ํ•ด๋‹น ํŒจํ„ด ๋„ค์ด๋ฐ์œผ๋กœ ์ด์ „ ํŒŒ์ผ์ด ๊ธฐ๋ก๋จ -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize> <!-- ํ•œ ํŒŒ์ผ์˜ ์ตœ๋Œ€ ์šฉ๋Ÿ‰ -->
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory> <!-- ํ•œ ํŒŒ์ผ์˜ ์ตœ๋Œ€ ์ €์žฅ ๊ธฐํ•œ -->
        </rollingPolicy>
    </appender>

<!--    <root level="INFO">-->
<!--        <appender-ref ref="STDOUT" />-->
<!--    </root>-->

    <!--  file์— ์ฐํž ๋กœ๊ทธ  -->
    <logger name="com.dot3company">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <logger name="org.springframework.web">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

</configuration>
  • ๋ฃจํŠธ์˜ ๋ ˆ๋ฒจ์„ xml ๋‚ด์— ์„ค์ •ํ•ด์ค˜์•ผ ๊ฐ logger ์˜ ๋ ˆ๋ฒจ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

<logger name="com.dot3company" level=โ€warnโ€ *additivity*="false" > ์ฒ˜๋Ÿผ additivity (์ƒ์†)์„ false ๋กœ ํ•˜๋ฉด ๋ฃจํŠธ์˜ info ๊ฐ€ ์•„๋‹Œ warn ๋กœ ๋ ˆ๋ฒจ์ด ์„ค์ •์ด ๋จ

  • xml ์ด ์•„๋‹Œ ํ”„๋กœํผํ‹ฐ์ฆˆ๋‚˜ yml ์— ๋ฃจํŠธ ์„ค์ •ํ•˜๋ฉด ์•ˆ๋˜๋”๋ผ

์ฐธ์กฐ

Logback ์œผ๋กœ ์‰ฝ๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ๋กœ๊ทธ ๊ด€๋ฆฌ๋ฅผ ํ•ด๋ณผ๊นŒ์š”? โš™๏ธ

[์Šคํ”„๋ง๋ถ€ํŠธ (5)] Spring Boot ๋กœ๊ทธ ์„ค์ •(1) - Logback

logstash-logback-encoder

GitHub - logfellow/logstash-logback-encoder: Logback JSON encoder and appenders

JSON ์œผ๋กœ ๋กœ๊ทธํŒŒ์ผ ์ถœ๋ ฅ

Spring boot LogBack for ELK Stack

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%thread{10}] [%X{traceId:-}] [%-5level] [%logger{0}:%line] - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>./logs/logback.log</file>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder"/><!-- json ํ˜•ํƒœ๋กœ ํŒŒ์ผ์— ๋กœ๊ทธ ๋‚จ๊น€ -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>./was-logs/info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <!-- ํ•ด๋‹น ํŒจํ„ด ๋„ค์ด๋ฐ์œผ๋กœ ์ด์ „ ํŒŒ์ผ์ด ๊ธฐ๋ก๋จ -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize> <!-- ํ•œ ํŒŒ์ผ์˜ ์ตœ๋Œ€ ์šฉ๋Ÿ‰ -->
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory> <!-- ํ•œ ํŒŒ์ผ์˜ ์ตœ๋Œ€ ์ €์žฅ ๊ธฐํ•œ -->
        </rollingPolicy>
    </appender>

<!--    <root level="INFO">-->
<!--        <appender-ref ref="STDOUT" />-->
<!--    </root>-->

    <!--  file์— ์ฐํž ๋กœ๊ทธ  -->
    <logger name="com.dot3company">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <logger name="org.springframework.web">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

</configuration>

Last updated