본문 바로가기
software engineering/java

Spring Boot - Logging

by _블로그 2021. 4. 26.

2.2.13 기준

 

Starters를 이용한다면 기본 logger는 logback이 됩니다. Java Util Logging, Commons Logging, Log4J, SLF4J 모두 동작하게 하는 dependent lib이 포함됩니다.

 

default logback 설정은 다음을 따릅니다.

github.com/spring-projects/spring-boot/blob/v2.2.13.RELEASE/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/base.xml

<included>
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
	<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
	<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
	<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
	<root level="INFO">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="FILE" />
	</root>
</included>

 

4.4.6. Custom Log Configuration

Spring이 log 관련 초기화를 온전히 진행할 수 있게 하기 위해서 설정파일의 이름에 '-spring' suffix를 붙이는 것을 권장합니다. e.g. logback-spring.xml

 

spring default 설정에서 사용하는 System Property(LOG_FILE, LOG_PATH ...)는 Spring Envirenment로도 대응이 됩니다.

docs.spring.io/spring-boot/docs/2.2.13.RELEASE/reference/html/spring-boot-features.html#boot-features-custom-log-configuration

 

위의 기본 file-appender.xml 설정은 아래와 같습니다.

<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<encoder>
			<pattern>${FILE_LOG_PATTERN}</pattern>
		</encoder>
		<file>${LOG_FILE}</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
			<fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
			<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
			<maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
			<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
		</rollingPolicy>
	</appender>

 

SizeAndTimeBasedRollingPolicy를 사용하는데, size 제한이 필요 없다면 아래와 같이 TimeBasedRollingPolicy만 사용하도록 설정해주면 됩니다.

<configuration>
	<!-- use Spring default values -->
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
	<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
	<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

	<!-- SizeAndTimeBasedRollingPolicy 사용하지 않고 TimeBasedRollingPolicy 사용하기 위해 별도로 설정 -->
	<appender name="FILE"
			  class="ch.qos.logback.core.rolling.RollingFileAppender">
		<encoder>
			<pattern>${FILE_LOG_PATTERN}</pattern>
		</encoder>
		<file>${LOG_FILE}</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
			<fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.gz}</fileNamePattern>
			<maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
			<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
		</rollingPolicy>
	</appender>
</configuration>
LIST

'software engineering > java' 카테고리의 다른 글

Spring Boot VS Quarkus  (0) 2022.11.07
이모지 제거  (0) 2022.02.04
gradle의 method 선언과 prop 선언의 차이  (0) 2021.04.16

댓글