【Java】SpringBoot接入阿里云日志服务Logback Appender

1、项目中引入依赖:

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>aliyun-log-logback-appender</artifactId>
    <version>0.1.18</version>
</dependency>

2、application.yml中日志文件配置如下:

logging:
  config: classpath:logback-spring.xml

3、logback-spring.xml中日志输出配置如下:

<configuration scan="true" scanPeriod="60 seconds">

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/lws-server-1.log</file>
        <encoder>
            <pattern>
                [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n
            </pattern>
            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
        </encoder>

        <!-- 设置为按照索引的方式滚动,定义文件名称的时候使用%i作为占位符,滚动后会会用角标替换 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>logs/social-server-%i.log</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>50MB</maxFileSize>
        </triggeringPolicy>
    </appender>
    <!-- Console output -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>
                [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n
            </pattern>
            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
        </encoder>
    </appender>

    <!--阿里云日志服务配置 测试环境-->
    <!--为了防止进程退出时,内存中的数据丢失,请加上此选项-->
    <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
    <appender name="ALIYUN_DEV" class="com.aliyun.openservices.log.logback.LoghubAppender">
        <!--必选项-->
        <!-- 账号及网络配置 -->
        <endpoint>cn-beijing-intranet.log.aliyuncs.com</endpoint>
        <accessKeyId>xxx</accessKeyId>
        <accessKeySecret>xxx</accessKeySecret>

        <!-- sls 项目配置 -->
        <project>lws-log-dev</project>
        <logStore>lws-logstore</logStore>
        <!--必选项 (end)-->

        <!-- 可选项 -->
        <topic>lws-log</topic>

        <!-- 可选项 详见 '参数说明'-->
        <totalSizeInBytes>104857600</totalSizeInBytes>
        <maxBlockMs>0</maxBlockMs>
        <ioThreadCount>8</ioThreadCount>
        <batchSizeThresholdInBytes>524288</batchSizeThresholdInBytes>
        <batchCountThreshold>4096</batchCountThreshold>
        <lingerMs>2000</lingerMs>
        <retries>10</retries>
        <baseRetryBackoffMs>100</baseRetryBackoffMs>
        <maxRetryBackoffMs>50000</maxRetryBackoffMs>

        <!-- 可选项 通过配置 encoder 的 pattern 自定义 log 的格式 -->
        <encoder>
            <pattern>%d %-5level [%thread] %logger{0}: %msg</pattern>
        </encoder>

        <!-- 可选项 设置 time 字段呈现的格式 -->
        <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
        <!-- 可选项 设置 time 字段呈现的时区 -->
        <timeZone>Asia/Shanghai</timeZone>
    </appender>

    <!--阿里云日志服务配置 正式环境-->
    <appender name="ALIYUN_PRO" class="com.aliyun.openservices.log.logback.LoghubAppender">
        <!--必选项-->
        <!-- 账号及网络配置 -->
        <endpoint>cn-beijing-intranet.log.aliyuncs.com</endpoint>
        <accessKeyId>xxx</accessKeyId>
        <accessKeySecret>xxx</accessKeySecret>

        <!-- sls 项目配置 -->
        <project>lws-log</project>
        <logStore>lws-logstore</logStore>
        <!--必选项 (end)-->

        <!-- 可选项 -->
        <topic>lws-log</topic>

        <!-- 可选项 详见 '参数说明'-->
        <totalSizeInBytes>104857600</totalSizeInBytes>
        <maxBlockMs>0</maxBlockMs>
        <ioThreadCount>8</ioThreadCount>
        <batchSizeThresholdInBytes>524288</batchSizeThresholdInBytes>
        <batchCountThreshold>4096</batchCountThreshold>
        <lingerMs>2000</lingerMs>
        <retries>10</retries>
        <baseRetryBackoffMs>100</baseRetryBackoffMs>
        <maxRetryBackoffMs>50000</maxRetryBackoffMs>

        <!-- 可选项 通过配置 encoder 的 pattern 自定义 log 的格式 -->
        <encoder>
            <pattern>%d %-5level [%thread] %logger{0}: %msg</pattern>
        </encoder>

        <!-- 可选项 设置 time 字段呈现的格式 -->
        <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
        <!-- 可选项 设置 time 字段呈现的时区 -->
        <timeZone>Asia/Shanghai</timeZone>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />

        <springProfile name="test">
            <appender-ref ref="ALIYUN_DEV" />
        </springProfile>
        <springProfile name="prod">
            <appender-ref ref="ALIYUN_PRO" />
        </springProfile>
    </root>

    <springProfile name="dev">
        <logger name="com.lws.test" level="DEBUG" />
    </springProfile>

    <springProfile name="test">
        <logger name="com.lws.test" level="DEBUG" />
    </springProfile>

    <springProfile name="prod">
        <logger name="com.lws.test" level="DEBUG" />
    </springProfile>

</configuration>

附:官方文档 https://github.com/aliyun/aliyun-log-logback-appender/blob/master/README_CN.md

 

已标记关键词 清除标记
相关推荐
实付 39.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值