colorful spring boot

spring boot의 banner / log 에 color 를 입혀보자

colorful spring boot

 

ASCII ART

문자 -> ASCII ART로 변환 : http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20

그림 -> ASCII ART로 변환 : http://picascii.com/

banner 변경

공식 문서 참조 : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html

resources 디렉토리에 ASCII ART 로 디자인한 내용을 담은 banner.txt 파일을 놔두면 된다.

color 입히는 방법은 아래 예제를 참조

banner.txt
${AnsiColor.RED} ____   ____   ${AnsiColor.BLACK}    _________ __                          __
${AnsiColor.RED}|    | /  _/   ${AnsiColor.BLACK}   /   _____/|  |__   ____ ______ ______ |__| ____    ____
${AnsiColor.RED}|        <     ${AnsiColor.BLACK}   \_____  \ |  |  \ /  _ \\  __ \\  __ \|  |/    \  / __ \
${AnsiColor.RED}|    |    \    ${AnsiColor.BLACK}   /        \|   Y  (  <_> )  |_> >  |_> >  |   |  \/ /_/  >
${AnsiColor.RED}|____|\____\   ${AnsiColor.BLACK}  /_________/|___|__/\____/|   __/|   __/|__|___|__/\___  /
${AnsiColor.RED}               ${AnsiColor.BLACK}                           |__|   |__|             /_____/
${AnsiColor.BLACK} ...Running ${AnsiColor.BRIGHT_RED}Kshop sample application${AnsiColor.BRIGHT_BLACK}

 

log coloring

logback(spring boot default logger) 의 log encoder 를 설정하면 된다.

설정 방법은 아래 예제를 참조

logback.groovy
appender('console', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = '%magenta(%d{yyyy-MM-dd HH:mm:ss.SSS}) %highlight(%5level) --- %yellow([%25thread]) %cyan(%logger{36}) - %msg%n'
    }
}
appender('rolling', RollingFileAppender) {
    file = "logs/daily.log"
    append = true
    rollingPolicy(TimeBasedRollingPolicy) {
        fileNamePattern = "/logs/daily.log.%d{yyyy-MM-dd}.gz"
        maxHistory = 10
    }
    encoder(PatternLayoutEncoder) {
        pattern = '%-5level %d{yyyy-MM-dd HH:mm:ss} [%thread] %logger{36} - %msg%n'
    }
}
root(INFO, ['console', 'rolling'])

spring boot

참고 문서

 

spring-boot

spring boot life-cycle

Cosysto Gimbh 라는 사람이 life-cycle에 대한 flow 를 아주 잘 그려놓았다.

 


 

실제 구현체 이름은 매우 길고 복잡하다. (e.g. ConfigurationWarningsApplicationContextInitializer)

spring application 내부에서 실제로 어떤 일이 일어나는지에 집중하도록 긴 이름은 짧게 축약하였다.

 

ApplicationContext 생성(=SpringApplication.run())

  • Listener 생성 및 시작
  • Environment 준비
  • Context 생성
  • Context 준비
    • Environment 세팅
    • 후처리(post process)
    • Initializer 초기화(apply initializer)
  • Context 로드
    • Source 로드
    • BeanDefinitionLoader 에 Source , Context 탑재
    • BeanDefinitionLoader 로드
  • 종료
    • Context Refresh

 

ApplicationContext 는 SpringApplication의 몸통에 해당하는 실제 instance의 추상화이다.

SpringApplication

SpringApplication 의 Entry point 는 반드시 특정 package를 지정하고, 그 하위에 위치시켜야 한다. (=Default package path에 entry point 시작 금지)

그렇지 않으면 ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.  이 발생한다.

 

@SpringBootApplication 을 사용하면 다음 Annotation 을 생략 가능하다.

Source Code


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
}

 

SpringApplicationRunListener

ConfigurableEnvironment

ConfigurableApplicationContext

 

실제로 호출되는 implementation 은 AnnotationConfigEmbddedWebApplicationContext 이다.