springboot logback cloudwatch에 보내기 본문

백앤드 개발일지/스프링부트

springboot logback cloudwatch에 보내기

giron 2021. 12. 1. 10:38
728x90

시험기간이라 오랜만에 포스팅하는 것 같다...ㅠ 빨리 시험 끝났으면~😗😗

 

logback을 파일로 저장해도 좋지만, aws에서 제공해주는 cloud watch를 이용하면 로컬에 파일을 저장 안 해도 되고, 시각적인 지표도 볼 수 있으므로 더욱 유용하다. 따라서 springboot에서 logback으로 cloudwatch에 보내서 aws에서 관리하도록 하겠다.

gradle

implementation 'ca.pjer:logback-awslogs-appender:1.4.0'

aws IAM 정책 설정

 - CloudWatchAgentServerPolicy 정책을 추가해서 EC2에 연결

Cloudwatch-agent 설치

$ wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

$ sudo rpm -U ./amazon-cloudwatch-agent.rpm

Cloudwatch-agent config.json 생성/설정

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json

wizard로 설정하는 방식은 https://brunch.co.kr/@topasvga/615 이 블로그를 보고 따라 하면 쉽다!

 

Cloudwatch-agent 중지

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop

설정한 거 적용하기

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

cloud-watch-agent 상태 확인하기 

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

정상적으로 연결되었는지 확인하기

cat /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log

file-warn-aws-appender.xml

<included>
    <appender name="FILE-WARN-AWS" class="ca.pjer.logback.AwsLogsAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <encoder>
            <pattern>${LOG_PATTERN_AWS}</pattern>
        </encoder>

        <logGroupName>로그 그룹이름</logGroupName>
        <logStreamUuidPrefix>warn/</logStreamUuidPrefix>
        <logRegion>ap-northeast-2</logRegion>
        <maxBatchLogEvents>50</maxBatchLogEvents>
        <maxFlushTimeMillis>30000</maxFlushTimeMillis>
        <maxBlockTimeMillis>5000</maxBlockTimeMillis>
        <retentionTimeDays>0</retentionTimeDays>
    </appender>
</included>

logback-spring.xml

<configuration>
    <timestamp key="BY_DATE" datePattern="yyyy-MM-dd"/>
    <property name="LOG_PATTERN"
              value="%-5level %d{yy-MM-dd HH:mm:ss}[%thread] [%logger{0}:%line] - %msg%n"/>
    <property name="LOG_CONSOLE_PATTERN"
              value="[%d{yyyy-MM-dd HH:mm:ss}:%-4relative] %green([%thread]) %highlight(%-5level) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/>
    <property name="LOG_PATTERN_AWS"
              value="날짜: %d{yy-MM-dd HH:mm:ss} 스레드: [%thread] 레벨:[%level] 클래스: [%C{0}.%M:#%L] 메세지: [%msg] %n"/>
    <property name="LOG_LOCATION" value="/home/ec2-user/app/logs"/>
    <property name="BACKUP_LOCATION" value="/home/ec2-user/app/logs"/>
    <property name="DEBUG_LOCATION" value="debug"/>
    <property name="INFO_LOCATION" value="info"/>
    <property name="WARN_LOCATION" value="warn"/>
    <property name="ERROR_LOCATION" value="error"/>
    <property name="MAX_FILE_SIZE" value="100MB"/>
    <property name="MAX_HISTORY" value="30"/>

    <springProfile name="local|test">
        <include resource="console-appender.xml"/>
        <logger name="com.minibeit" level="DEBUG">
            <appender-ref ref="CONSOLE"/>
        </logger>
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>

    <springProfile name="dev">
        <include resource="console-appender.xml"/>
        <include resource="file-debug-appender.xml"/>
        <include resource="file-info-appender.xml"/>
        <include resource="file-warn-appender.xml"/>
        <include resource="file-error-appender.xml"/>
        <include resource="file-info-aws-appender.xml"/>
        <include resource="file-error-aws-appender.xml"/>
        <include resource="file-warn-aws-appender.xml"/>
        <include resource="file-debug-aws-appender.xml"/>

        <logger name="com.minibeit" level="DEBUG">
            <appender-ref ref="CONSOLE"/>
        </logger>
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
            <appender-ref ref="FILE-DEBUG"/>
            <appender-ref ref="FILE-INFO"/>
            <appender-ref ref="FILE-WARN"/>
            <appender-ref ref="FILE-ERROR"/>
            <appender-ref ref="FILE-INFO-AWS"/>
            <appender-ref ref="FILE-ERROR-AWS"/>
            <appender-ref ref="FILE-WARN-AWS"/>
            <appender-ref ref="FILE-DEBUG-AWS"/>
        </root>
    </springProfile>
    
    <! --- ... prod profile --->
    
    </configure>

 

728x90
Comments