### Install Volcengine Log Service Go SDK Source: https://www.volcengine.com/docs/6470/117661 Use 'go get' to download the SDK. For Go 1.16+, 'go install' is recommended. ```go go get -u github.com/volcengine/volc-sdk-golang go ``` -------------------------------- ### 下载火山引擎日志服务 Go SDK Source: https://www.volcengine.com/docs/6470/117661?lang=zh 使用 `go get` 命令下载火山引擎日志服务 Go SDK。对于 Go 1.16 及以上版本,推荐使用 `go install` 命令。 ```go go get -u github.com/volcengine/volc-sdk-golang ``` -------------------------------- ### Log Topic Management Examples (Go) Source: https://www.volcengine.com/docs/6470/116567 Code examples for managing log topics, covering creation, modification, viewing, and deletion. These are available in the GitHub example projects. ```Go 日志主题管理​| 日志主题管理的示例代码,包括创建日志主题、修改日志主题、查看日志主题和删除日志主题。 ``` -------------------------------- ### C++ SDK Example Project - Common API Calls Source: https://www.volcengine.com/docs/6470/1119991 This example project demonstrates common API calls for the Volcengine Log Service C++ SDK, including creating log projects, creating log topics, and writing logs. It serves as a practical guide for integrating the SDK into your C++ applications. ```cpp #include #include #include #include "tls_client.h" #include "tls_define.h" using namespace std; using namespace VolcengineTls; int main() { // Replace with your actual access key, secret key, and region string accessKey = "YOUR_ACCESS_KEY"; string secretKey = "YOUR_SECRET_KEY"; string region = "YOUR_REGION"; // e.g., "cn-beijing" string endpoint = "your_tls_endpoint"; // e.g., "https://tls.cn-beijing.volces.com" // Initialize TLS client TLSClient client(accessKey, secretKey, region, endpoint); // --- Example: Create Log Project --- cout << "--- Creating Log Project ---" << endl; CreateProjectRequest createProjectReq; createProjectReq.projectName = "my_cpp_project"; createProjectReq.projectDesc = "My C++ Project Description"; createProjectReq.tags = {{"key1", "value1"}, {"key2", "value2"}}; CreateProjectResponse createProjectRes; ErrorCode err = client.CreateProject(createProjectReq, createProjectRes); if (err.Code() == 0) { cout << "Project created successfully. Project ID: " << createProjectRes.project_id << endl; } else { cerr << "Failed to create project. Error Code: " << err.Code() << ", Error Message: " << err.Message() << endl; return 1; } // --- Example: Create Log Topic --- cout << "\n--- Creating Log Topic ---" << endl; CreateTopicRequest createTopicReq; createTopicReq.projectName = createProjectReq.projectName; createTopicReq.topicName = "my_cpp_topic"; createTopicReq.topicDesc = "My C++ Topic Description"; createTopicReq.auto_split = false; createTopicReq.max_split_num = 5; createTopicReq.ttl = 30; createTopicReq.tags = {{"topic_tag_key", "topic_tag_value"}}; CreateTopicResponse createTopicRes; err = client.CreateTopic(createTopicReq, createTopicRes); if (err.Code() == 0) { cout << "Topic created successfully. Topic ID: " << createTopicRes.topic_id << endl; } else { cerr << "Failed to create topic. Error Code: " << err.Code() << ", Error Message: " << err.Message() << endl; // Clean up project if topic creation fails client.DeleteProject(createProjectRes.project_id); return 1; } // --- Example: Write Logs --- cout << "\n--- Writing Logs ---" << endl; WriteLogsRequest writeLogsReq; writeLogsReq.project_id = createProjectRes.project_id; writeLogsReq.topic_id = createTopicRes.topic_id; // Create log items vector logs; Log log1; log1.content = "This is the first log message."; log1.time = time(NULL); // Current time log1.contents["key1"] = "value1"; logs.push_back(log1); Log log2; log2.content = "This is the second log message with a field."; log2.time = time(NULL) + 1; // Slightly later time log2.contents["key2"] = "value2"; log2.contents["level"] = "info"; logs.push_back(log2); writeLogsReq.logs = logs; WriteLogsResponse writeLogsRes; err = client.WriteLogs(writeLogsReq, writeLogsRes); if (err.Code() == 0) { cout << "Logs written successfully. Count: " << writeLogsRes.count << endl; } else { cerr << "Failed to write logs. Error Code: " << err.Code() << ", Error Message: " << err.Message() << endl; // Clean up project and topic if log writing fails client.DeleteTopic(createProjectRes.project_id, createTopicRes.topic_id); client.DeleteProject(createProjectRes.project_id); return 1; } // --- Example: Delete Log Topic --- cout << "\n--- Deleting Log Topic ---" << endl; err = client.DeleteTopic(createProjectRes.project_id, createTopicRes.topic_id); if (err.Code() == 0) { cout << "Topic deleted successfully." << endl; } else { cerr << "Failed to delete topic. Error Code: " << err.Code() << ", Error Message: " << err.Message() << endl; // Clean up project if topic deletion fails client.DeleteProject(createProjectRes.project_id); return 1; } // --- Example: Delete Log Project --- cout << "\n--- Deleting Log Project ---" << endl; err = client.DeleteProject(createProjectRes.project_id); if (err.Code() == 0) { cout << "Project deleted successfully." << endl; } else { cerr << "Failed to delete project. Error Code: " << err.Code() << ", Error Message: " << err.Message() << endl; return 1; } cout << "\nDemo finished successfully." << endl; return 0; } ``` -------------------------------- ### Node.js package.json with SDK dependency Source: https://www.volcengine.com/docs/6470/116992 This is an example of the package.json file after installing the Volcengine Node.js SDK. ```json { "name": "nodejs-doc", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@volcengine/openapi": "^1.2.2" } } ``` -------------------------------- ### Log Project Management Examples (Go) Source: https://www.volcengine.com/docs/6470/116567 Code examples for managing log projects, including creation, modification, viewing, and deletion. These snippets are part of the GitHub example projects. ```Go 日志项目管理​| 日志项目管理的示例代码,包括创建日志项目、修改日志项目、查看日志项目和删除日志项目。 ``` -------------------------------- ### Java SDK: Consume Logs with Consumer Group Source: https://www.volcengine.com/docs/6470/1172144 This example demonstrates the overall process of creating a consumer group and a consumer using the Java SDK to consume logs. It initializes the client, configures consumer settings, starts consumption, and includes a placeholder for custom log processing logic. ```Java package com.volcengine.example.tls.demo; import java.util.ArrayList; import java.util.List; import com.volcengine.model.tls.consumer.ConsumerConfig; import com.volcengine.model.tls.exception.LogException; import com.volcengine.model.tls.pb.PutLogRequest; import com.volcengine.service.tls.consumer.Consumer; import com.volcengine.service.tls.consumer.ConsumerImpl; import com.volcengine.service.tls.consumer.LogProcessor; // 您需要定义一个实现LogProcessor接口的类 public class ConsumerDemo implements LogProcessor { public static void main(String[] args) throws LogException, InterruptedException { // 初始化客户端,推荐通过环境变量动态获取火山引擎密钥等身份认证信息,以免 AccessKey 硬编码引发数据安全风险。详细说明请参考https://www.volcengine.com/docs/6470/1166455 // 使用 STS 时,ak 和 sk 均使用临时密钥,且设置 VOLCENGINE_TOKEN;不使用 STS 时,VOLCENGINE_TOKEN 部分传空 ConsumerConfig config = new ConsumerConfig(System.getenv("VOLCENGINE_ENDPOINT"), System.getenv("VOLCENGINE_REGION"), System.getenv("VOLCENGINE_ACCESS_KEY_ID"), System.getenv("VOLCENGINE_ACCESS_KEY_SECRET"), System.getenv("VOLCENGINE_TOKEN")); // 请配置您的日志项目ID config.setProjectID("your-project-id"); // 请配置您待消费的日志主题ID列表 config.setTopicIDList(new ArrayList(){ add("your-topic-id"); }); // 请配置您的消费组名称 config.setConsumerGroupName("java-consumer-group"); // 请配置消费者名称 config.setConsumerName("java-consumer"); // 实例化ConsumerImpl,调用consumer.start()开始持续消费 Consumer consumer = new ConsumerImpl(config, new ConsumerDemo()); consumer.start(); // 可通过调用consumer.stop()来结束消费组消费 Thread.sleep(10000); consumer.stop(); } /** * 您需要根据业务需要,自行实现这里的process方法,用于处理每次消费得到的LogGroupList * 下面给出了逐个打印消费到的日志的代码示例 */ @Override public void process(String topicID, int shardID, PutLogRequest.LogGroupList logGroupList) { System.out.println(topicID + " --- " + shardID); System.out.println(logGroupList.getLogGroupsCount()); int count = 0; List logGroups = logGroupList.getLogGroupsList(); for (PutLogRequest.LogGroup logGroup: logGroups) { List logs = logGroup.getLogsList(); for (PutLogRequest.Log log: logs) { count++; System.out.println("*** Count = " + count + " ***"); List logContents = log.getContentsList(); for (PutLogRequest.LogContent logContent: logContents) { System.out.println(logContent.getKey() + ": " + logContent.getValue()); } System.out.println(); } } } } ``` -------------------------------- ### Install and Start LogCollector Source: https://www.volcengine.com/docs/6470/72007 Execute this command to install and automatically start the LogCollector process. Required parameters include region, endpoint, secret_id, and secret_key. Optional parameters like label, memory, and cpu can also be set. ```shell sudo ./logcollector.sh install --region --endpoint --secret_id --secret_key --label