- [spring boot 2.1.7] 개발 준비

  - [spring boot 2.1.7] profile 및 logback 설정

  - [spring boot 2.1.7] JSP 설정

  - [spring boot 2.1.7] thymelef 설정

  - [spring boot 2.1.7] mybatis - mysql 5.7 연동

  - [spring boot 2.1.7] hibernate - mysql 5.7 연동 설정

 

 

소스 : https://github.com/yamoe/spring-boot-example/tree/master/02.base/arti

 

 

* 개발, 배포 환경별로 설정 나누기 : profile

- build.gradle 추가

sourceSets {
	ext.profile = (!project.hasProperty('profile') || !profile) ? 'dev' : profile
	println "current profile : ${profile}"
	main {
		java {
			srcDirs "src/main/java"
		}
		resources {
			srcDirs "src/main/resources", "src/main/resources/env/${profile}/"
		}
	}
}

 

- 구성

 

- 빌드 :

  -Pprofile 을 통해 어떤 환경의 설정 파일을 사용할지 지정 (dev, alpha, prod)

  아래 명령은 env/prod/application.yml 의 설정 파일을 사용하도록 지정함. 

> gradlew clean build -Pprofile=prod 

 

- 실행

 공통 설정 파일인 application-common.yml 를 사용하도록 common profile 활성화

> java -jar -Dspring.profiles.active=common arti.jar 

 

- 참고: 

    https://perfectacle.github.io/2017/09/23/Spring-boot-gradle-profile/

    https://effectivesquid.tistory.com/entry/Spring-boot-profile-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

 

 

 

* 로그(logback) 설정

- logback-spring.xml 에 appender 및 spring profile 설정

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

    <property name="LOGS" value="./logs" />

    <springProfile name="console-logging">
        <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} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M [%L]- %msg%n</Pattern>
            </layout>
        </appender>
    </springProfile>



    <springProfile name="file-logging">
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder>
                <Pattern>%d{yyyy-mm-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M [%L]- %msg%n</Pattern>
            </encoder>
            <file>${LOGS}/spring-boot-logger.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <fileNamePattern>${LOGS}/spring-boot-logger-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
                <maxFileSize>10MB</maxFileSize>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
        </appender>
    </springProfile>

    <root>
        <springProfile name="console-logging">
            <appender-ref ref="CONSOLE"/>
        </springProfile>

        <springProfile name="file-logging">
            <appender-ref ref="FILE"/>
        </springProfile>
    </root>

</configuration>

 

- 각 환경별 application.xml에 아래와 같이 사용할 로거 기술

spring:
  profiles:
    include:
      - console-logging
      - file-logging

 

- 로깅 테스트를 위해 controller 작성

http://localhost:9999/api/values 로 확인

package com.example.arti.api;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping(value = "api")
public class ApiController {

    @GetMapping(value = "values")
    public String[] GetValues() {
        log.info("Values");
        return new String[]{"value1", "value2"};
    }

}

 

- 참고:

  https://meetup.toast.com/posts/149

  https://www.baeldung.com/spring-boot-logging