[Spring Boot] Spring Boot custom 로거 생성 및 롤링 설정

double-oh·2021년 8월 22일
0

application.yml 파일

...(생략)...

# 로깅 설정
logging:
  config: classpath:logging-config.xml

...(생략)...

logging-config.xml 파일

<configuration>
    <timestamp key="CURRENT_DATE" datePattern="yyyy-MM-dd"/>
    <property name="LOG_DIR" value="/data/logs"/>
    <property name="LOG_PATH_NAME" value="${LOG_DIR}/test.log"/>
    <property name="TEST_LOG_PATH_NAME" value="${LOG_DIR}/test.log"/>
    <property name="MAX_FILE_SIZE" value="50MB"/>
    <property name="MAX_HISTORY" value="30"/>

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

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH_NAME}</file>
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] [%F]%M\(%L\) : %m%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH_NAME}_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} | %m%n</pattern>
        </layout>
    </appender>

    <appender name="test-log" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${TEST_LOG_PATH_NAME}</file>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <provider class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/>
            <timestampPattern>yyyy-MM-dd'T'HH:mm:ss.SSS</timestampPattern>
            <fieldNames>
                <timestamp>timestamp</timestamp>
            </fieldNames>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${TEST_LOG_PATH_NAME}_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} | %m%n</pattern>
        </layout>
    </appender>

    <logger name="com.example" level="DEBUG" additivity="false">
        <appender-ref ref="console"/>
        <appender-ref ref="FILE"/>
    </logger>

    <logger name="test-logger" level="INFO" additivity="false">
        <appender-ref ref="test-log"/>
    </logger>

    <root level="INFO">
        <appender-ref ref="console"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>
  • customizing한 로거를 이용하여 로그 생성
...(생략)...

private final static Logger testLogger = LoggerFactory.getLogger("test-logger");

testLogger.info("test");

...(생략)...
profile
Yes, Code Wins Arguments!!

0개의 댓글