すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:SDK を使用して SPL に基づくログを取得する

最終更新日:Jun 18, 2026

SDK を使用して、コンシュームプロセッサ (SPL) に基づくログを取得できます。Java、Python、Go のコード例を以下に示します。

前提条件

  • Resource Access Management (RAM) ユーザーが作成され、必要な権限が付与されています。詳細については、「RAM ユーザーの作成と権限付与」をご参照ください。

  • ALIBABA_CLOUD_ACCESS_KEY_ID および ALIBABA_CLOUD_ACCESS_KEY_SECRET 環境変数が設定されています。詳細については、「Linux、macOS、Windows での環境変数の設定」をご参照ください。

    重要
    • Alibaba Cloud アカウント (root ユーザー) の AccessKey ペアはすべての API オペレーションに対する権限を持ちます。API オペレーションの呼び出しや日常的な運用管理 (O&M) には、RAM ユーザーの AccessKey ペアを使用することを推奨します。

    • プロジェクトコード内に AccessKey ID または AccessKey Secret を含めないでください。いずれかが漏洩した場合、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。

  • コンシューマープロセッサーを作成する

コード例

Java

  1. Simple Log Service SDK をインストールします。Java プロジェクトのルートディレクトリで pom.xml ファイルを開き、以下の Maven 依存関係を追加してください。詳細については、「Java SDK のインストール」をご参照ください。

    Simple Log Service SDK for Java はバージョン 0.6.126 以降である必要があります。
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>2.5.0</version>
    </dependency>
    <!-- Import the Simple Log Service SDK -->
    <dependency>
      <groupId>com.aliyun.openservices</groupId>
      <artifactId>aliyun-log</artifactId>
      <version>0.6.126</version>
    </dependency>
  2. PullLogsWithSPLDemo.java ファイルを作成します。この例では、PullLog 操作を呼び出して SPL に基づくログデータを取得します。

    import com.aliyun.openservices.log.Client;
    import com.aliyun.openservices.log.common.*;
    import com.aliyun.openservices.log.request.PullLogsRequest;
    import com.aliyun.openservices.log.response.ListShardResponse;
    import com.aliyun.openservices.log.response.PullLogsResponse;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class PullLogsWithSPLDemo {
        // The endpoint of Simple Log Service. This example uses the China (Hangzhou) region. Replace it with the endpoint of your region.
        private static final String endpoint = "cn-hangzhou.log.aliyuncs.com";
        // This example obtains the AccessKey ID and AccessKey secret from environment variables.
        private static final String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        private static final String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // The project name. Replace it with your actual project name.
        private static final String project = "ali-project-test";
        // The Logstore name. Replace it with your actual Logstore name.
        private static final String logStore = "test-logstore";
    
        public static void main(String[] args) throws Exception {
            // The ID of the Consume Processor.
            String processorName = "processor-test";
            // Create a Simple Log Service client.
            Client client = new Client(endpoint, accessKeyId, accessKeySecret);
            // Query the shards of the Logstore.
            ListShardResponse resp = client.ListShard(project, logStore);        System.out.printf("%s has %d shards\n", logStore, resp.GetShards().size());
            Map<Integer, String> cursorMap = new HashMap<>();
            for (Shard shard : resp.GetShards()) {
                int shardId = shard.getShardId();
                // Start consumption from the beginning and get a cursor. To start from the end, use Consts.CursorMode.END.
                cursorMap.put(shardId, client.GetCursor(project, logStore, shardId, Consts.CursorMode.BEGIN).GetCursor());
            }
            try {
                while (true) {
                    // Get logs from each shard.
                    for (Shard shard : resp.GetShards()) {
                        int shardId = shard.getShardId();
                        PullLogsRequest request = new PullLogsRequest(project, logStore, shardId, 1000, cursorMap.get(shardId));
                        // The ID of the Consume Processor.
                        request.setProcessor(processorName);
                        PullLogsResponse response = client.pullLogs(request);
                        // Logs are in log groups. Split them as needed.
                        List<LogGroupData> logGroups = response.getLogGroups();
                        System.out.printf("Get %d logGroup from logstore:%s:\tShard:%d\n", logGroups.size(), logStore, shardId);
    
                        // Move the cursor after you process the pulled logs.
                        cursorMap.put(shardId, response.getNextCursor());
                    }
                }
            } catch (LogException e) {
                System.out.println("error code :" + e.GetErrorCode());
                System.out.println("error message :" + e.GetErrorMessage());
                throw e;
            }
        }
    }
  3. main 関数を実行し、出力を確認します。

    Get 41 logGroup from logstore:test-logstore:	Shard:0
    Get 49 logGroup from logstore:test-logstore:	Shard:1
    Get 43 logGroup from logstore:test-logstore:	Shard:0
    Get 39 logGroup from logstore:test-logstore:	Shard:1
    ... ...

Python

  1. Simple Log Service SDK をインストールします。spl_demo という名前のプロジェクトフォルダを作成し、そのフォルダ内で以下のコマンドを実行してください。詳細については、「Simple Log Service SDK for Python のインストール」をご参照ください。

    Alibaba Cloud Simple Log Service SDK はバージョン 0.9.28 以降である必要があります。
    pip install -U aliyun-log-python-sdk
  2. spl_demo フォルダ内に main.py ファイルを作成します。このコードはコンシューマーグループを作成し、指定された Logstore からデータを取得するコンシューマースレッドを開始します。

    # encoding: utf-8
    
    import time
    import os
    from aliyun.log import *
    
    def main():
        # The endpoint of Simple Log Service. This example uses the China (Hangzhou) region. Replace it with the endpoint of your region.
        endpoint = 'cn-hangzhou.log.aliyuncs.com'
        # This example obtains the AccessKey ID and AccessKey secret from environment variables.
        access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
        access_key = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
        # The project name. Replace it with your actual project name.
        project_name = 'ali-project-test'
        # The Logstore name. Replace it with your actual Logstore name.
        logstore_name = 'test-logstore'
        # The ID of the Consume Processor.
        processor = "processor-test"
        init_cursor = 'end'
        log_group_count = 10
    
        # Create a Simple Log Service client.
        client = LogClient(endpoint, access_key_id, access_key)
    
        cursor_map = {}
        # List the shards of the logstore.
        res = client.list_shards(project_name, logstore_name)
        res.log_print()
        shards = res.shards
    
        # Get the initial cursor.
        for shard in shards:
            shard_id = shard.get('shardID')
            res = client.get_cursor(project_name, logstore_name, shard_id, init_cursor)
            cursor_map[shard_id] = res.get_cursor()
    
        # Read data from each shard in a loop.
        while True:
            for shard in shards:
                shard_id = shard.get('shardID')
                res = client.pull_logs(project_name, logstore_name, shard_id, cursor_map.get(shard_id), log_group_count,
                                       processor=processor)
                res.log_print()
                if cursor_map[shard_id] == res.next_cursor:
                    # only for debug
                    time.sleep(3)
                else:
                    cursor_map[shard_id] = res.next_cursor
    
    
    if __name__ == '__main__':
        main()
  3. main 関数を実行し、出力を確認します。

    ListShardResponse:
    headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '335', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 26 Feb 2025 09:46:17 GMT', 'x-log-time': '1740563177', 'x-log-requestid': '67BEE2E9132069E22A1F967D'}
    res: [{'shardID': 0, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1737010019}, {'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '80000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1737010019}]
    PullLogResponse
    next_cursor MTczNz********c3ODgyMjQ0MQ==
    log_count 0
    headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/x-protobuf', 'Content-Length': '1', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 26 Feb 2025 09:46:17 GMT', 'x-log-cursor-time': '0', 'x-log-end-of-cursor': '1', 'x-log-failedlines': '0', 'x-log-rawdatacount': '0', 'x-log-rawdatalines': '0', 'x-log-rawdatasize': '0', 'x-log-read-last-cursor': '0', 'x-log-resultlines': '0', 'x-log-time': '1740563177', 'x-log-bodyrawsize': '0', 'x-log-compresstype': 'gzip', 'x-log-count': '0', 'x-log-cursor': 'MTczNzAx********ODgyMjQ0MQ==', 'x-log-requestid': '67BEE2E974CA9ABCE7DDC7D6'}
    detail: []
    PullLogResponse
    next_cursor MTczNz********c3OTg5NzE3NA==
    log_count 0
    headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/x-protobuf', 'Content-Length': '1', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 26 Feb 2025 09:46:21 GMT', 'x-log-cursor-time': '0', 'x-log-end-of-cursor': '1', 'x-log-failedlines': '0', 'x-log-rawdatacount': '0', 'x-log-rawdatalines': '0', 'x-log-rawdatasize': '0', 'x-log-read-last-cursor': '0', 'x-log-resultlines': '0', 'x-log-time': '1740563181', 'x-log-bodyrawsize': '0', 'x-log-compresstype': 'gzip', 'x-log-count': '0', 'x-log-cursor': 'MTczNzAx********OTg5NzE3NA==', 'x-log-requestid': '67BEE2EDF2B58CF1756526EF'}
    detail: []
    PullLogResponse
    ... ...

Go

  1. Simple Log Service SDK をインストールします。spl_demo という名前のプロジェクトフォルダを作成し、そのフォルダ内で以下のコマンドを実行してください。詳細については、「Go SDK のインストール」をご参照ください。

    Alibaba Cloud Simple Log Service SDK はバージョン 0.1.107 以降である必要があります。
    go get -u github.com/aliyun/aliyun-log-go-sdk
  2. spl_demo フォルダ内に main.go ファイルを作成します。このコードはコンシューマーグループを作成し、指定された Logstore からデータを取得するコンシューマースレッドを開始します。

    package main
    
    import (
    	"fmt"
    	"os"
    	"time"
    
    	sls "github.com/aliyun/aliyun-log-go-sdk"
    )
    
    func main() {
    	client := &sls.Client{
    		AccessKeyID:     os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    		AccessKeySecret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    		Endpoint:        "cn-chengdu.log.aliyuncs.com",
    	}
    
    	project := "ali-project-test"
    	logstore := "test-logstore"
    	initCursor := "end"
        // The ID of the Consume Processor.
    	consumeProcessor := "ali-test-consume-processor"
    	shards, err := client.ListShards(project, logstore)
    	if err != nil {
    		fmt.Println("ListShards error", err)
    		return
    	}
    
    	shardCursorMap := map[int]string{}
    	for _, shard := range shards {
    		cursor, err := client.GetCursor(project, logstore, shard.ShardID, initCursor)
    		if err != nil {
    			fmt.Println("GetCursor error", shard.ShardID, err)
    			return
    		}
    		shardCursorMap[shard.ShardID] = cursor
    	}
    
    	for {
    		for _, shard := range shards {
    			pullLogRequest := &sls.PullLogRequest{
    				Project:          project,
    				Logstore:         logstore,
    				ShardID:          shard.ShardID,
    				LogGroupMaxCount: 10,
    				Processor:        consumeProcessor,
    				Cursor:           shardCursorMap[shard.ShardID],
    			  }
    			lg, nextCursor, err := client.PullLogsV2(pullLogRequest)
    			fmt.Println("shard: ", shard.ShardID, "loggroups: ", len(lg.LogGroups), "nextCursor: ", nextCursor)
    			if err != nil {
    				fmt.Println("PullLogsV2 error", shard.ShardID, err)
    				return
    			}
    			shardCursorMap[shard.ShardID] = nextCursor
    			if len(lg.LogGroups) == 0 {
    				// only for debug
    				time.Sleep(time.Duration(3) * time.Second)
    			}
    		}
    	}
    }
  3. main 関数を実行し、出力を確認します。

    shard:  0 loggroups:  41 nextCursor:  MTY5Mz*******TIxNjcxMDcwMQ==
    shard:  1 loggroups:  49 nextCursor:  MTY5Mz*******DYwNDIyNDQ2Mw==
    shard:  0 loggroups:  43 nextCursor:  MTY5Mz*******TIxNjcxMDcwMQ==
    shard:  1 loggroups:  39 nextCursor:  MTY5Mz*******DYwNDIyNDQ2Mw==
    ... ...