콘텐츠로 이동

2021 07 27

2021-07-27

Logging과 Profile 전략

  • 참고: https://meetup.toast.com/posts/149
  • Logback 확장 - 기존의 logback에서는 웹 앱 가동하면 클래스패쓰에서 logback.xml을 검색해 로그백 초기화 - 스프링 부트에서는 스프링 구동되고 읽기 때문에 logback-spring.xml 사용해서 가져올 수 있음 - 덕분에 application.xml 가져올 수 있어 굿
  • Logger 등록과 level 설정 - 스프링부트는 yml에서 logger와 level 설정 가능
  • 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>
    

    - <include ~~defaults.xml/> : console log pattern, file log pattern의 기본값 정의됨, tomcat/hibernate의 모듈 log level 설정 - <property ~~> : application.yml의 설정 읽어옴 - <include ~~/console-appender.xml> : console appender 설정 읽어옴 - <include ~~/file-appender.xml> : file appender 설정 읽어옴

  • application.yml의 프로퍼티 가져오기 - 원격의 로그 서버로 로그 보내기 가능 - 로그 서버의 주소를 application.yml에서 설정하자 - logserver.host: http://logserver.com
  • Profile별 환경 설정 - spring.profiles.active에서 지정한 프로파일에 따라 조건 추가 가능 - 로깅 기능을 나누어 프로파일에서 조합하여 사용해보자
    <springProfile name="console-logging">
      <appender name="CONSOLE" class="..."></appender>
    </springProfile>
    
    <springProfile name="file-logging">
        <appender name="FILE" class="..."></appender>
    </springProfile>
    
    <springProfile name="remote-logging">
      <appender name="REMOTE_LOG_SERVER" class="..."></appender>
    </springProfile>
    
    <root>
      <springProfile name="console-logging">
        <appender-ref ref="CONSOLE"/>
      </springProfile>
    
      <springProfile name="file-logging">
        <appender-ref ref="FILE"/>
      </springProfile>
    
      <springProfile name="remote-logging">
        <appender-ref ref="REMOTE_LOG_SERVER"/>
      </springProfile>
    </root>
    

    - 조합을 뚝딱하여 배포환경에 맞게 필요한 설정 갖다 쓰자 - spring.profiles.include

    application-dev.yml
    spring.profiles.include: console-logging
    
    application-alpha.yml
    spring.profiles.include: file-logging
    
    application-beta.yml
    spring.profiles.include: file-logging,remote-logging