spring

[Spring Boot] 설정 파일(application.properties, logback.xml) 개발, 운영 분리 설정

박진만 2023. 3. 21. 12:41
반응형

Spring Boot 개발 환경에 따라 설정 파일을 분리해서 관리하는 방법

1. application.properties
- spring.profiles.active 에 profiles 값 설정 : dev(개발), release(운영) 으로 분리함
- 개발, 운영 환경에서 공통적으로 사용하는 설정은 application.properties 파일에 작성.

2. application-dev.properties
- spring.profiles.active 값이 dev 로 되어 있을 경우 해당 파일을 설정 파일로 인식
- 개발 환경에서 사용하는 설정 application-dev.properties 파일에 작성

3. application-release.properties
- spring.profiles.active 값이 release 로 되어 있을 경우 해당 파일을 설정 파일로 인식
- 운영 환경에서 사용하는 설정 application-release.properties 파일에 작성

4. logback 설정도 spring.profiles.active 값에 따라 분기할 수 있다.
- application-dev.properties 파일에 logging.config=classpath:logback-spring-dev.xml 설정
- application-release.properties 파일에 logging.config=classpath:logback-spring-release.xml 설정
- logback.xml 또는 logback-spring.xml 파일이 있을 경우 삭제
- logback-spring-dev.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>

    <!-- pattern -->
    <property name="LOG_PATTERN_DEFAULT" value="[%d{yyyy:MM:dd HH:mm:ss}] %-5level --- [%thread] %logger{35} : %msg %n"/>
    
    <property name="LOG_PATH" value="C:\\Users\\abc\\git\\abcd\\logs"/>
    <property name="LOG_FILE_NAME" value="abcd"/>

    <!-- CONSOLE appender -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN_DEFAULT}</pattern>
        </encoder>
    </appender>
    
    <!-- file appender -->
    <appender name="INFO_DEFAULT_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/${LOG_FILE_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}_%i.zip</fileNamePattern>
            <!-- <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}_%i.zip</fileNamePattern> -->
            <!-- 파일당 최고 용량 kb, mb, gb -->
        	<maxFileSize>100MB</maxFileSize>
            <!-- 일자별 로그파일 최대 보관주기(~일), 해당 설정일 이상된 파일은 자동으로 제거-->
            <maxHistory>30</maxHistory>
            <!-- 로그 파일 아카이브 저장소의 최대크기를 지정. totalSizeCap을 초과하면 가장 오래된 파일 삭제. maxHistory 1순위, totalSizeCap 2순위-->
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN_DEFAULT}</pattern>
        </encoder>
    </appender>

    <appender name="ERROR_DEFAULT_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${LOG_PATH}/${LOG_FILE_NAME}-error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}-error.%d{yyyy-MM-dd}_%i.zip</fileNamePattern>
            <!-- <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}-error.%d{yyyy-MM-dd}_%i.zip</fileNamePattern> -->
            <!-- 파일당 최고 용량 kb, mb, gb -->
        	<maxFileSize>100MB</maxFileSize>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN_DEFAULT}</pattern>
        </encoder>
    </appender>
    
    <logger name="com.abcd" additivity="false">
    	<level value="DEBUG"/>
    	<appender-ref ref="CONSOLE"/>
    	<appender-ref ref="INFO_DEFAULT_FILE_APPENDER"/>
    	<appender-ref ref="ERROR_DEFAULT_FILE_APPENDER"/>
    </logger>
    <logger name="org.springframework" additivity="false">
    	<level value="INFO"/>
    	<appender-ref ref="CONSOLE"/>
    	<appender-ref ref="INFO_DEFAULT_FILE_APPENDER"/>
    	<appender-ref ref="ERROR_DEFAULT_FILE_APPENDER"/>
    </logger>
    <root level="ERROR">
    	<appender-ref ref="CONSOLE"/>
    	<appender-ref ref="INFO_DEFAULT_FILE_APPENDER"/>
    	<appender-ref ref="ERROR_DEFAULT_FILE_APPENDER"/>
    </root>
     
</configuration>

- logback-spring-release.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>

    <!-- pattern -->
    <property name="LOG_PATTERN_DEFAULT" value="[%d{yyyy:MM:dd HH:mm:ss}] %-5level --- [%thread] %logger{35} : %msg %n"/>
    
    <property name="LOG_PATH" value="/home/abcd/log"/>
    <property name="LOG_FILE_NAME" value="abcd"/>

    <!-- CONSOLE appender -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN_DEFAULT}</pattern>
        </encoder>
    </appender>
    
    <!-- file appender -->
    <appender name="INFO_DEFAULT_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/${LOG_FILE_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}_%i.zip</fileNamePattern>
            <!-- <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}_%i.zip</fileNamePattern> -->
            <!-- 파일당 최고 용량 kb, mb, gb -->
        	<maxFileSize>100MB</maxFileSize>
            <!-- 일자별 로그파일 최대 보관주기(~일), 해당 설정일 이상된 파일은 자동으로 제거-->
            <maxHistory>30</maxHistory>
            <!-- 로그 파일 아카이브 저장소의 최대크기를 지정. totalSizeCap을 초과하면 가장 오래된 파일 삭제. maxHistory 1순위, totalSizeCap 2순위-->
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN_DEFAULT}</pattern>
        </encoder>
    </appender>

    <appender name="ERROR_DEFAULT_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${LOG_PATH}/${LOG_FILE_NAME}-error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}-error.%d{yyyy-MM-dd}_%i.zip</fileNamePattern>
            <!-- <fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}-error.%d{yyyy-MM-dd}_%i.zip</fileNamePattern> -->
            <!-- 파일당 최고 용량 kb, mb, gb -->
        	<maxFileSize>100MB</maxFileSize>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN_DEFAULT}</pattern>
        </encoder>
    </appender>
    
    <logger name="com.abcd" additivity="false">
    	<level value="ERROR"/>
    	<appender-ref ref="CONSOLE"/>
    	<appender-ref ref="INFO_DEFAULT_FILE_APPENDER"/>
    	<appender-ref ref="ERROR_DEFAULT_FILE_APPENDER"/>
    </logger>
    <logger name="org.springframework" additivity="false">
    	<level value="ERROR"/>
    	<appender-ref ref="CONSOLE"/>
    	<appender-ref ref="INFO_DEFAULT_FILE_APPENDER"/>
    	<appender-ref ref="ERROR_DEFAULT_FILE_APPENDER"/>
    </logger>
    <root level="ERROR">
    	<appender-ref ref="CONSOLE"/>
    	<appender-ref ref="INFO_DEFAULT_FILE_APPENDER"/>
    	<appender-ref ref="ERROR_DEFAULT_FILE_APPENDER"/>
    </root>
     
</configuration>
반응형