[Logback] JSON Layout Format 적용하기.
ITWeb/개발일반 2021. 10. 5. 18:52Springboot 로 프로젝트 생성 후 로깅 하면서 로그 포멧을 JSON 으로 적용 하기 위한 내용을 기억 차원에서 작성 합니다.
[logback.xml]
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</timestampFormat>
<timestampFormatTimezoneId>Asia/Seoul</timestampFormatTimezoneId>
<appendLineSeparator>true</appendLineSeparator>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>application/application-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</timestampFormat>
<timestampFormatTimezoneId>Asia/Seoul</timestampFormatTimezoneId>
<appendLineSeparator>true</appendLineSeparator>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
</appender>
<logger name="org.springframework" level="error" additivity="false" />
<logger name="org.apache" level="error" additivity="false" />
<logger name="com.zaxxer" level="error" additivity="false" />
<logger name="io.netty" level="error" additivity="false" />
<logger name="reactor" level="error" additivity="false" />
<logger name="com.xxxxx" level="debug" additivity="false">
<appender-ref ref="FILE" />
</logger>
<root level="debug">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
[build.gradle]
compile "ch.qos.logback.contrib:logback-jackson:${props.getProperty('logback-jackson')}"
compile "ch.qos.logback.contrib:logback-json-classic:${props.getProperty('logback-json-classic')}"
- 여기서 사용 버전은 0.1.5 입니다.
사용한 timezone 은 ETC/UTC 가 아닌 Asia/Seoul 로 설정 했습니다.