You need to enable JavaScript to run this app.
Torch Log Service

Torch Log Service

Copy page
Download PDF
Data consumption and shipping
Consume logs using ConsumeLogs
Copy page
Download PDF
Consume logs using ConsumeLogs
TLS provides the ConsumeLogs API for consuming logs, enabling real-time access to log data collected on the server. This topic describes how to call the ConsumeLogs API using the TLS SDK to achieve sequential reading and writing of complete log data.​
Background information​
TLS can act as a log data transfer hub, providing log forwarding features similar to Kafka. By using the log consumption APIs and multi-language SDKs provided by TLS, you can use applications in Go and other languages as consumers to access log data in real time. For server-side APIs related to log consumption, see ConsumeLogs. For information about multi-language SDKs provided by TLS, see Overview.​
Notice​
TLS offers the ConsumerGroup feature through the SDK. You can consume log data using consumer groups. When consuming logs with a consumer group, TLS automatically balances consumer capacity and progress, and allocates Shards. For details, see Consumption data by consumption group.​
Code examples​
Refer to the following code example to create a ConsumeLogs.go file, call the ConsumeLogs API to read log data, and consume log data in real time.​
​
import (​
"fmt"​
"time"​
. "github.com/volcengine/volc-sdk-golang/service/tls"​
"github.com/volcengine/volc-sdk-golang/service/tls/pb"​
)​
func consumeLogs(topicID string, shardID int) {​
client := NewClient(os.Getenv("LOG_TEST_ENDPOINT"), os.Getenv("LOG_TEST_ACCESS_KEY_ID"),​
// LOG_TEST_SECURITY_TOKEN is a temporary security token obtained through IAM STS. When using a temporary token, you also need to pass temporary AK and SK. For more information, see https://www.volcengine.com/docs/6470/160162. When not using a temporary token, set LOG_TEST_SECURITY_TOKEN to empty.​
os.Getenv("LOG_TEST_ACCESS_KEY_SECRET"), os.Getenv("LOG_TEST_SECURITY_TOKEN"),​
os.Getenv("LOG_TEST_REGION"))​
// Get the initial log cursor​
beginCursorResp, err := client.DescribeCursor(&DescribeCursorRequest{​
TopicID: topicID,​
ShardID: shardID,​
From: "begin",​
})​
if err != nil {​
panic(err.Error())​
}​
// Get the last log cursor​
endCursorResp, err := client.DescribeCursor(&DescribeCursorRequest{​
TopicID: topicID,​
ShardID: shardID,​
From: "end",​
})​
if err != nil {​
panic(err.Error())​
}​
// Get the log cursor for a specific time​
timestampCursorResp, err := client.DescribeCursor(&DescribeCursorRequest{​
TopicID: topicID,​
ShardID: shardID,​
From: "1661418000",​
})​
if err != nil {​
panic(err.Error())​
}​
var logGroupCount = 10​
// Consume ten groups of logs​
consumedLogs, err := client.ConsumeLogs(&ConsumeLogsRequest{​
TopicID: topicID,​
ShardID: shardID,​
Cursor: timestampCursorResp.Cursor,​
LogGroupCount: &logGroupCount,​
})​
if err != nil {​
panic(err.Error())​
}​
fmt.Println(consumedLogs)​
// Consume ten groups of logs and set the compression format to lz4 (currently only lz4 is supported)​
var compressionType = "lz4"​
consumedLogs, err = client.ConsumeLogs(&ConsumeLogsRequest{​
TopicID: topicID,​
ShardID: shardID,​
Cursor: timestampCursorResp.Cursor,​
LogGroupCount: &logGroupCount,​
Compression: &compressionType,​
})​
if err != nil {​
panic(err.Error())​
}​
// Specify the start and end consumption logs​
consumedLogs, err = client.ConsumeLogs(&ConsumeLogsRequest{​
TopicID: topicID,​
ShardID: shardID,​
Cursor: beginCursorResp.Cursor,​
EndCursor: &endCursorResp.Cursor,​
LogGroupCount: &logGroupCount,​
Compression: &compressionType,​
})​
if err != nil {​
panic(err.Error())​
}​
}​
​
​
Last updated: 2026.03.23 11:52:09