首页 » 文章 » logback 用appender接入阿里云日志

阿里云的日志系统比elk还是好用多了,而且支持像sql那样查询日志。接入方法如下


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    ​
    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
    <springProperty scope="context" name="mytopic" source="spring.application.name"/>
    <springProperty scope="context" name="myprofile" source="spring.profiles.active"/>
    <!-- Example for logging into the build folder of your project -->
    <property name="LOG_FILE" value="/data/logs/${mytopic}.log"/>​

    <!-- You can override this to have a custom pattern -->
    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr([${springAppName:-},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]){yellow} %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

    <!-- Appender to log to console -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <!-- Minimum logging level to be presented in the console logs-->
            <level>DEBUG</level>
        </filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>

    <!-- Appender to log to file -->​
    <appender name="flatfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>
    ​

    <appender name="loghubAppender1" class="com.aliyun.openservices.log.logback.LoghubAppender">
        <!--必选项-->
        <!-- 账号及网络配置 -->
        <endpoint>xxxx-3.log.aliyuncs.com</endpoint>
        <accessKeyId>xxxxxxx</accessKeyId>
        <accessKey>xxxxxxxx</accessKey>

        <!-- sls 项目配置 -->
        <projectName>thor1108</projectName>
        <logstore>thor-${myprofile}</logstore>
        <!--必选项 (end)-->

        <!-- 可选项 -->
        <topic>${mytopic}</topic>

        <!-- 可选项 详见 '参数说明'-->
        <packageTimeoutInMS>3000</packageTimeoutInMS>
        <logsCountPerPackage>4096</logsCountPerPackage>
        <logsBytesPerPackage>3145728</logsBytesPerPackage>
        <memPoolSizeInByte>104857600</memPoolSizeInByte>
        <retryTimes>3</retryTimes>
        <maxIOThreadSizeInPool>8</maxIOThreadSizeInPool>

        <!-- 可选项 通过配置 encoder 的 pattern 自定义 log 的格式 -->
        <encoder>
            <pattern>
                    {
                        "severity": "%level",
                        "service": "${springAppName:-}",
                        "trace": "%X{X-B3-TraceId:-}",
                        "span": "%X{X-B3-SpanId:-}",
                        "exportable": "%X{X-Span-Export:-}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "rest": "%message"
                        }
            </pattern>
        </encoder>

        <!-- 可选项 设置时间格式 -->
        <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
        <!-- 可选项 设置时区 -->
        <!--<timeZone>Asia/Shanghai</timeZone>-->

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
        </filter>
    </appender>
    ​
    <root level="INFO">
        <appender-ref ref="console"/>
        <!-- uncomment this to have also JSON logs -->
        <appender-ref ref="loghubAppender1"/>
        <appender-ref ref="flatfile"/>
    </root>
</configuration>

注意里面的traceid 使用的spring cloud sleuth ,引入依赖即可

添加新评论