logback接入到阿里雲sls日誌服務

Ruthless發表於2022-08-31

一、阿里雲配置
0、sls日誌應用主介面

1、建立Project

2、建立logStore
建立logStore用於日誌資料儲存

 

 

3、開啟索引
開啟日誌庫索引,tajia-log(Project) => tajia-api(logsearch) => 開啟索引(右上角)

 

二、nijia-test專案配置
1、引入pom

<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、logback.xml檔案配置

    <!-- 控制檯 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    
    <!-- 引入sls日誌 -->
    <appender name="aliyun" class="com.aliyun.openservices.log.logback.LoghubAppender">
        <!-- Required parameters -->
        <!-- Configure account and network  -->
        <endpoint>cn-shenzhen.log.aliyuncs.com</endpoint>
      <!-- 填寫sls key金鑰 -->
        <accessKeyId>**********</accessKeyId>
        <accessKeySecret>*****************</accessKeySecret>

        <!-- Configure sls -->
        <!--Project名稱 -->
        <project>tajia-log</project>
        <!--logStore名稱 -->
        <logStore>tajia-api</logStore>
        <!-- Required parameters(end) -->

        <!-- Optional parameters -->
        <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>

        <!-- Optional parameters -->
<!--        <encoder>-->
<!--            <pattern>%d %-5level [%thread] %logger{0}: %msg</pattern>-->
<!--        </encoder>-->

        <!--  Optional parameters -->
        <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
        <!--  Optional parameters -->
        <timeZone>UTC</timeZone>
    </appender>
<root level="INFO">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="ASYNC_FILE" />
    <appender-ref ref="ASYNC_ERROR_FILE" />
    <appender-ref ref="aliyun" />
</root>

 

三、啟動專案,在阿里雲點查詢,可以看到日誌已輸出
登入阿里雲,檢視sls日誌應用 => tajia-log(Project) => tajia-api(logsearch)

 

 

相關文章