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

Torch Log Service

Copy page
Download PDF
Ship logs to Splunk
Ship logs to Splunk using SDK
Copy page
Download PDF
Ship logs to Splunk using SDK
This topic introduces the basic concepts and sample code for shipping Syslog logs to Splunk using TLS.​
Features overview​
TLS provides the capability to ship Syslog log data to Splunk. After consuming data with the ConsumeLogs API of TLS, the data is parsed into the corresponding Syslog format and then written to Splunk.​
To call the ConsumeLogs API using the TLS SDK, see Consume logs using ConsumeLogs.​
Notice​
  • Currently, RFC5424 and RFC3164 Syslog protocols are supported.​
  • When sending Syslog log data to the Splunk platform via TCP, the Splunk platform does not remove the original priority information in the event, that is, the priority information included in the Syslog data (such as the priority defined by the PRI field) is retained. For details, see the Splunk website.​
  • An available Splunk endpoint is required to receive data in advance.​
Code examples​
Refer to the following examples to obtain consumer group configurations, configure TLS topics, set up the syslog client, and use RFC5424 Syslog as an example to print each key-value pair of every consumed log entry one by one.​
​
package tls​
​
import (​
"context"​
"crypto/tls"​
"fmt"​
tlsConsumer "github.com/volcengine/volc-sdk-golang/service/tls/consumer"​
"github.com/volcengine/volc-sdk-golang/service/tls/pb"​
"github.com/volcengine/volc-sdk-golang/service/tls/siem"​
"log"​
"os"​
"strconv"​
"time"​
)​
​
func ConsumerLogsToSyslogClient() error {​
// Get the default configuration for the consumer group​
consumerCfg := tlsConsumer.GetDefaultConsumerConfig()​
// Configure your Endpoint, Region, AccessKeyID, AccessKeySecret, and other essential information​
consumerCfg.Endpoint = os.Getenv("VOLCENGINE_ENDPOINT")​
consumerCfg.Region = os.Getenv("VOLCENGINE_REGION")​
consumerCfg.AccessKeyID = os.Getenv("VOLCENGINE_ACCESS_KEY_ID")​
consumerCfg.AccessKeySecret = os.Getenv("VOLCENGINE_ACCESS_KEY_SECRET")​
// Configure your project ID and log topic ID list​
consumerCfg.ProjectID = "<YOUR-PROJECT-ID>"​
consumerCfg.TopicIDList = []string{"<YOUR-TOPIC-ID>"}​
// Configure the name of your consumer group (if you haven't created one, the SDK will automatically create a consumer group with the specified name)​
consumerCfg.ConsumerGroupName = "<CONSUMER-GROUP-NAME>"​
// Configure the consumer name (consumers in the same consumer group must use unique names)​
consumerCfg.ConsumerName = "<CONSUMER_NAME>"​
​
// Set up the syslog client​
syslogClient := siem.NewSyslogClient()​
// Set client configurations​
syslogClient.Host = os.Getenv("SPLUNK_HOST")​
syslogClient.Port, _ = strconv.Atoi(os.Getenv("SPLUNK_PORT_UDP"))​
// For SSL authentication, configure as needed. Note: Do not confuse the TLS here with the logging TLS.​
syslogClient.SslSettings = &tls.Config{}​
​
// Start the client​
err := syslogClient.Connect()​
if err != nil {​
log.Fatalf("syslog client connect error %v:", err)​
}​
​
// Define the log consumption function. You can implement a function to process LogGroupList based on your business needs​
// Using RFC5424 syslog as an example, the following code demonstrates how to print each key-value pair of every log consumed, one by one.​
var handleLogsFunc = func(topicID string, shardID int, l *pb.LogGroupList) {​
for _, logGroup := range l.LogGroups {​
for _, tlsLog := range logGroup.Logs {​
​
// If using RFC3164, use NewSyslogFormatRFC3164​
syslogRFC5424 := siem.NewSyslogFormatRFC5424()​
​
for _, content := range tlsLog.Contents {​
switch content.Key {​
// Update the fields to match the actual fields in the logs​
case "pri":​
syslogRFC5424.Priority = content.Value​
// Update the fields to match the actual fields in the logs​
case "version":​
syslogRFC5424.Version = content.Value​
// Update the fields to match the actual fields in the logs​
case "timestamp":​
syslogRFC5424.Timestamp, _ = time.Parse(time.RFC3339, content.Value)​
// Update the fields to match the actual fields in the logs​
case "hostname":​
syslogRFC5424.Hostname = content.Value​
// Update the fields to match the actual fields in the logs​
case "appName":​
syslogRFC5424.AppName = content.Value​
// Update the fields to match the actual fields in the logs​
case "pid":​
syslogRFC5424.Pid = content.Value​
// Update the fields to match the actual fields in the logs​
case "msgId":​
syslogRFC5424.MsgId = content.Value​
// Update the fields to match the actual fields in the logs​
case "message":​
syslogRFC5424.Message = content.Value​
}​
}​
​
syslogRFC5424Format, err := syslogRFC5424.FormatSyslogMessageWithRFC5424()​
if err != nil {​
// Handle formatting errors here​
log.Printf("NOTICE: syslogRFC5424.FormatSyslogMessageWithRFC5424() failed: %v", err)​
}​
​
// When sending fails, it will automatically retry once​
err = syslogClient.Send(syslogRFC5424Format)​
if err != nil {​
// Handle logic for send failure here​
return​
}​
​
fmt.Println("Send successfully")​
}​
}​
}​
​
tlsConsumerIns, err := tlsConsumer.NewConsumer(context.TODO(), consumerCfg, handleLogsFunc)​
if err != nil {​
log.Fatalf("NewConsumer error %v", err)​
}​
​
// Start consuming logs​
if err := tlsConsumerIns.Start(); err != nil {​
return fmt.Errorf("start consumer failed, error is %v", err)​
}​
​
// Control log consumption exit based on your actual scenario​
time.Sleep(300 * time.Second)​
​
_ = syslogClient.Close()​
tlsConsumerIns.Stop()​
​
return nil​
}​
​
​
Last updated: 2026.05.06 14:26:39