Skip to content

Commit 028fad4

Browse files
author
raylchen
committed
Optimize: Modify cls log module
1 parent 685c238 commit 028fad4

9 files changed

Lines changed: 107 additions & 76 deletions

File tree

README.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,34 @@ Detailed usage examples can be found in [CLS examples](./examples/)。
3636
1. Import repository
3737

3838
In the project's WORKSPACE file, import the cpp-cls-logging repository and its dependencies:
39-
```
40-
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
41-
42-
git_repository(
43-
name = "trpc_cpp",
44-
remote = "https://github.com/trpc-group/trpc-cpp.git",
45-
branch = "main",
46-
)
47-
48-
load("@trpc_cpp//trpc:workspace.bzl", "trpc_workspace")
49-
trpc_workspace()
50-
51-
git_repository(
52-
name = "cpp-cls-logging",
53-
remote = "https://github.com/trpc-ecosystem/cpp-logging-cls.git",
54-
branch = "main",
55-
)
56-
57-
load("@cpp-cls-logging//trpc:workspace.bzl", "logging_cls_workspace")
58-
logging_cls_workspace()
59-
```
39+
40+
```bzl
41+
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
42+
43+
git_repository(
44+
name = "trpc_cpp",
45+
remote = "https://github.com/trpc-group/trpc-cpp.git",
46+
branch = "main",
47+
)
48+
49+
load("@trpc_cpp//trpc:workspace.bzl", "trpc_workspace")
50+
trpc_workspace()
51+
52+
git_repository(
53+
name = "cpp-cls-logging",
54+
remote = "https://github.com/trpc-ecosystem/cpp-logging-cls.git",
55+
branch = "main",
56+
)
57+
58+
load("@cpp-cls-logging//trpc:workspace.bzl", "logging_cls_workspace")
59+
logging_cls_workspace()
60+
```
6061

6162
2. Import plugin
6263

6364
In the target where CLS is needed, import the "@cpp-cls-logging//trpc/logging/cls:cls_log_api" dependency. For example:
64-
```
65+
66+
```bzl
6567
cc_binary(
6668
name = "helloworld_server",
6769
srcs = ["helloworld_server.cc"],
@@ -79,6 +81,7 @@ Not supported yet.
7981
## Plugin Configuration
8082

8183
To use the Etcd plugin, you must add the plugin configuration to the framework configuration file:
84+
8285
```yaml
8386
plugins:
8487
log:
@@ -110,20 +113,20 @@ The following is a detailed explanation of these configuration items:
110113

111114
The CLS plugin provides a plugin registration interface ::trpc::cls::Init(), and users need to call this interface before starting the framework.
112115

113-
For server scenarios, users need to call the TrpcApp::RegisterPlugins function in the service startup:
116+
1. For server scenarios, users need to call the TrpcApp::RegisterPlugins function in the service startup:
114117

115-
```cpp
116-
#include "trpc/logging/cls/cls_log_api.h"
118+
```cpp
119+
#include "trpc/logging/cls/cls_log_api.h"
117120

118-
class HelloworldServer : public ::trpc::TrpcApp {
119-
public:
120-
...
121-
int RegisterPlugins() override {
122-
::trpc::logging::cls::Init();
123-
return 0;
124-
}
125-
};
126-
```
121+
class HelloworldServer : public ::trpc::TrpcApp {
122+
public:
123+
...
124+
int RegisterPlugins() override {
125+
::trpc::logging::cls::Init();
126+
return 0;
127+
}
128+
};
129+
```
127130
128131
2. For pure client scenarios, you need to call it after starting the framework configuration initialization and before starting other framework modules:
129132
@@ -149,6 +152,7 @@ For server scenarios, users need to call the TrpcApp::RegisterPlugins function i
149152
* Before using the CLS plugin, you need to ensure that the CLS service is correctly activated and configured. In addition, you also need to know how to create log topics and configure log delivery rules on the Tencent Cloud console. The following is a brief introduction to these two aspects:
150153
151154
### Activate and configure CLS service
155+
152156
1. Log in to the Tencent Cloud Console [TencentCloud](https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F) and find the Cloud Log Service[CLS](https://cloud.tencent.com/product/cls) in the product list.
153157
154158
2. In the CLS console, create a new log set.
@@ -158,13 +162,15 @@ For server scenarios, users need to call the TrpcApp::RegisterPlugins function i
158162
4. In the newly created log topic, configure log delivery rules. You can set the delivery target (e.g., COS, CDN, etc.), delivery frequency, and delivery file format according to your needs.
159163
160164
### Get the Topic ID of the log topic
165+
161166
1. Log in to the Tencent Cloud Console [TencentCloud](https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F) and find the Cloud Log Service[CLS](https://cloud.tencent.com/product/cls) in the product list.
162167
163168
2. Enter the page of the log topic you just created.
164169
165170
3. On the basic information page of the log topic, find the Topic ID of the log topic. Add this Topic ID to the plugin configuration to send log data to that topic.
166171
167172
### Special Tips
173+
168174
1. To ensure that log data can be correctly sent to the CLS platform, please make sure your Tencent Cloud account has sufficient permissions. When using the CLS plugin, you need to configure the SecretId and SecretKey in the yaml file. These two parameters can be created and obtained in the API key management page of the Tencent Cloud Console[TencentCloud](https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F).
169175
170176
2. Please note that sending log data to the CLS platform may incur certain costs. For specific cost standards, please refer to the billing instructions in the Tencent Cloud official document [TencentCloud](https://buy.cloud.tencent.com/pricing).

README.zh_CN.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ ClsSink 类是一个自定义的 spdlog 日志接收器,负责将日志发送
3535

3636
1. 引入仓库
3737

38-
在项目的WORKSPACE文件中,引入cpp-cls-logging仓库及其依赖:
39-
```
38+
在项目的WORKSPACE文件中,引入 `cpp-cls-logging` 仓库及其依赖:
39+
40+
```bzl
4041
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
4142

4243
git_repository(
@@ -60,8 +61,9 @@ ClsSink 类是一个自定义的 spdlog 日志接收器,负责将日志发送
6061

6162
2. 引入插件
6263

63-
在需要用到CLS的目标中引入“@cpp-cls-logging//trpc/logging/cls:cls_log_api”依赖。例如:
64-
```
64+
在需要用到 CLS 的目标中引入“@cpp-cls-logging//trpc/logging/cls:cls_log_api”依赖。例如:
65+
66+
```bzl
6567
cc_binary(
6668
name = "helloworld_server",
6769
srcs = ["helloworld_server.cc"],
@@ -79,6 +81,7 @@ ClsSink 类是一个自定义的 spdlog 日志接收器,负责将日志发送
7981
## 插件配置
8082

8183
要使用Etcd插件,必须在框架配置文件中加上插件配置:
84+
8285
```yaml
8386
plugins:
8487
log:
@@ -110,20 +113,20 @@ plugins:
110113

111114
CLS插件提供了插件注册的接口 ::trpc::cls::Init(),用户需要在框架启动前调用该接口进行初始化。
112115

113-
对于服务端场景,用户需要在服务启动的 TrpcApp::RegisterPlugins 函数中调用:
116+
1. 对于服务端场景,用户需要在服务启动的 TrpcApp::RegisterPlugins 函数中调用:
114117

115-
```cpp
116-
#include "trpc/logging/cls/cls_log_api.h"
118+
```cpp
119+
#include "trpc/logging/cls/cls_log_api.h"
117120

118-
class HelloworldServer : public ::trpc::TrpcApp {
119-
public:
120-
...
121-
int RegisterPlugins() override {
122-
::trpc::cls::Init();
123-
return 0;
124-
}
125-
};
126-
```
121+
class HelloworldServer : public ::trpc::TrpcApp {
122+
public:
123+
...
124+
int RegisterPlugins() override {
125+
::trpc::cls::Init();
126+
return 0;
127+
}
128+
};
129+
```
127130
128131
2. 对于纯客户端场景,需要在启动框架配置初始化后,框架其他模块启动前调用:
129132
@@ -149,6 +152,7 @@ CLS插件提供了插件注册的接口 ::trpc::cls::Init(),用户需要在框
149152
* 在使用CLS插件之前,您需要确保已正确开通并配置了CLS服务。此外,您还需要了解如何在腾讯云控制台上创建日志主题和配置日志投递规则。以下是有关这两个方面的简要说明:
150153
151154
### 开通并配置CLS服务
155+
152156
1. 登录腾讯云控制台 [TencentCloud](https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F) 在产品列表中找到云日志服务[CLS](https://cloud.tencent.com/product/cls)。
153157
154158
2. 在CLS控制台中,创建一个新的日志集。
@@ -158,13 +162,15 @@ CLS插件提供了插件注册的接口 ::trpc::cls::Init(),用户需要在框
158162
4. 在新创建的日志主题中,配置日志投递规则。您可以根据需要设置投递的目标(例如COS、CDN等)、投递频率、投递文件格式等参数。
159163
160164
### 获取日志主题的Topic ID
165+
161166
1. 登录腾讯云控制台 [TencentCloud](https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F) 在产品列表中找到云日志服务[CLS](https://cloud.tencent.com/product/cls)。
162167
163168
2. 进入刚刚创建的日志主题页面。
164169
165170
3. 在日志主题的基本信息页面,找到该日志主题的Topic ID。将此Topic ID添加到插件配置中,以便将日志数据发送到该主题。
166171
167172
### 特别提示
173+
168174
1. 为了确保日志数据能够正确地发送到CLS平台,请确保您的腾讯云账户具有足够的权限。在使用CLS插件时,您需要在配置SecretId和SecretKey在yaml文件中,这两个参数可以在腾讯云控制台[TencentCloud](https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F)的API密钥管理页面中创建和获取。
169175
170176
2. 请注意,发送到CLS平台的日志数据可能会产生一定的费用。具体的费用标准,请参考腾讯云官方文档[TencentCloud](https://buy.cloud.tencent.com/pricing)中的计费说明。

examples/client/trpc_cpp_fiber.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ plugins:
99
log:
1010
default:
1111
- name: default
12-
min_level: 2
12+
min_level: 2 # 0-trace, 1-debug, 2-info, 3-warn, 4-error, 5-critical
1313
format: "[%Y-%m-%d %H:%M:%S.%e] [thread %t] [%l] [%@] %v"
14-
mode: 2
14+
mode: 2 # 1-Synchronous, 2-Asynchronous, 3-Extreme-Speed
1515
sinks:
1616
stdout: # sink of the local console
1717
eol: true # Whether each output is automatically wrapped. Default line feed

examples/server/trpc_cpp_fiber.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ plugins:
2424
log:
2525
default:
2626
- name: default
27-
min_level: 2
27+
min_level: 2 # 0-trace, 1-debug, 2-info, 3-warn, 4-error, 5-critical
2828
format: "[%Y-%m-%d %H:%M:%S.%e] [thread %t] [%l] [%@] %v"
29-
mode: 2
29+
mode: 2 # 1-Synchronous, 2-Asynchronous, 3-Extreme-Speed
3030
sinks:
3131
stdout: # sink of the local console
3232
eol: true # Whether each output is automatically wrapped. Default line feed

trpc/logging/cls/cls_log.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ namespace trpc::cls {
2626

2727
std::unordered_map<std::string, uint32_t> logger_plungin_id_map;
2828

29-
void SetLoggerPlunginId(const std::string& logger_name, uint32_t plugin_id) {
29+
void SetLoggerPluginId(const std::string& logger_name, uint32_t plugin_id) {
3030
logger_plungin_id_map[logger_name] = plugin_id;
3131
}
3232

33-
uint32_t GetPlunginIdFromLogger(const std::string& logger_name) { return logger_plungin_id_map[logger_name]; }
33+
uint32_t GetPluginIdFromLogger(const std::string& logger_name) { return logger_plungin_id_map[logger_name]; }
3434

35-
std::unordered_map<std::string, uint32_t>& GetLoggerPlunginIdMap() { return logger_plungin_id_map; }
35+
std::unordered_map<std::string, uint32_t>& GetLoggerPluginIdMap() { return logger_plungin_id_map; }
3636

37-
void ClsLog::initSDK() {
37+
void ClsLog::InitSDK() {
3838
// Init cls-sdk client.
3939
sdk_config_.set_maxsendworkercount(1);
4040
sdk_config_.set_endpoint(endpoint_);
@@ -49,7 +49,7 @@ void ClsLog::initSDK() {
4949
cls_sink_->Start();
5050
}
5151

52-
bool ClsLog::initSpdLogger() {
52+
bool ClsLog::InitSpdLogger() {
5353
auto formatter =
5454
std::make_unique<spdlog::pattern_formatter>(spdlog::pattern_time_type::local, spdlog::details::os::default_eol);
5555
cls_sink_->set_formatter(std::move(formatter));
@@ -81,13 +81,13 @@ int ClsLog::Init() noexcept {
8181
cls_sink_ = std::make_shared<ClsSinkMt>();
8282

8383
// Initialize spdlog logger configuration
84-
if (!initSpdLogger()) {
84+
if (!InitSpdLogger()) {
8585
TRPC_FMT_ERROR("Init spdlog logger fail!");
8686
return -1;
8787
}
8888

8989
// Initialize the cls-sdk configuration
90-
initSDK();
90+
InitSDK();
9191

9292
return 0;
9393
}

trpc/logging/cls/cls_log.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ namespace trpc::cls {
3636
/// @brief Set the plugin ID for a given logger name.
3737
/// @param logger_name The name of the logger.
3838
/// @param plugin_id The ID of the plugin associated with the logger.
39-
void SetLoggerPlunginId(const std::string& logger_name, uint32_t plugin_id);
39+
void SetLoggerPluginId(const std::string& logger_name, uint32_t plugin_id);
4040

4141
/// @brief Get the plugin ID associated with a given logger name.
4242
/// @param logger_name The name of the logger.
4343
/// @return The plugin ID associated with the logger name.
44-
uint32_t GetPlunginIdFromLogger(const std::string& logger_name);
44+
uint32_t GetPluginIdFromLogger(const std::string& logger_name);
4545

4646
/// @brief Get the map of logger names to plugin IDs.
4747
/// @return A reference to the unordered_map containing logger names as keys and plugin IDs as values.
48-
std::unordered_map<std::string, uint32_t>& GetLoggerPlunginIdMap();
48+
std::unordered_map<std::string, uint32_t>& GetLoggerPluginIdMap();
4949

5050
/// @brief Adds key-value pairs to the context's filter data for each logger plugin.
5151
/// @tparam T The type of the context object.
@@ -63,14 +63,14 @@ std::unordered_map<std::string, uint32_t>& GetLoggerPlunginIdMap();
6363
/// @endcode
6464
template <typename T, typename... Args>
6565
void WithFields(T& context, Args&&... key_value_pairs) {
66-
auto plugin_id_map = GetLoggerPlunginIdMap();
66+
auto plugin_id_map = GetLoggerPluginIdMap();
6767
for (const auto& [logger_name, plugin_id] : plugin_id_map) {
6868
auto* data_map = context->template GetFilterData<std::unordered_map<std::string, std::string>>(
69-
GetPlunginIdFromLogger(logger_name.c_str()));
69+
GetPluginIdFromLogger(logger_name.c_str()));
7070
if (!data_map) {
7171
std::unordered_map<std::string, std::string> new_data_map;
7272
(new_data_map.emplace(std::forward<Args>(key_value_pairs)), ...);
73-
context->SetFilterData(GetPlunginIdFromLogger(logger_name.c_str()), std::move(new_data_map));
73+
context->SetFilterData(GetPluginIdFromLogger(logger_name.c_str()), std::move(new_data_map));
7474
} else {
7575
(data_map->emplace(std::forward<Args>(key_value_pairs)), ...);
7676
}
@@ -79,11 +79,11 @@ void WithFields(T& context, Args&&... key_value_pairs) {
7979

8080
class ClsLog : public Logging {
8181
public:
82-
std::string Name() const { return "cls-log"; }
82+
std::string Name() const override { return "cls-log"; }
8383

84-
std::string LoggerName() const { return logger_name_; }
84+
const std::string& LoggerName() const override { return logger_name_; }
8585

86-
std::string Version() const { return "1.0.0"; }
86+
std::string Version() const { return "1.0.0"; }
8787

8888
explicit ClsLog(const ClsLogSinkConfig& sink_config)
8989
: logger_name_(sink_config.logger_name),
@@ -92,7 +92,7 @@ class ClsLog : public Logging {
9292
endpoint_(sink_config.endpoint),
9393
secret_id_(sink_config.secret_id),
9494
secret_key_(sink_config.secret_key) {
95-
SetLoggerPlunginId(logger_name_, this->GetPluginID());
95+
SetLoggerPluginId(logger_name_, this->GetPluginID());
9696
Init();
9797
}
9898

@@ -154,10 +154,10 @@ class ClsLog : public Logging {
154154

155155
private:
156156
// Initialize the CLS SDK
157-
void initSDK();
157+
void InitSDK();
158158

159159
// Initialize the spdlog logger instance
160-
bool initSpdLogger();
160+
bool InitSpdLogger();
161161

162162
private:
163163
// logger configuration
@@ -181,4 +181,4 @@ class ClsLog : public Logging {
181181

182182
using ClsLogPtr = RefPtr<ClsLog>;
183183

184-
} // namespace trpc::cls
184+
} // namespace trpc::cls

trpc/logging/cls/cls_log_api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bool Init() {
2424
return -1;
2525
}
2626
for (const auto& config : config_list) {
27-
TrpcPlugin::GetInstance()->RegisterLogging(MakeRefCounted<ClsLog>(std::move(config)));
27+
TrpcPlugin::GetInstance()->RegisterLogging(MakeRefCounted<ClsLog>(config));
2828
}
2929

3030
return true;

trpc/logging/cls/cls_log_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ class ClsLogTest : public ::testing::Test {
3737
return;
3838
}
3939

40+
ASSERT_TRUE(config_list.size() == 1);
41+
42+
auto& cls_conf = config_list[0];
43+
cls_conf.Display();
44+
ASSERT_STREQ(cls_conf.logger_name.c_str(), "default");
45+
ASSERT_TRUE(cls_conf.mode == 2);
46+
ASSERT_TRUE(cls_conf.logs_per_request == 100);
47+
ASSERT_TRUE(cls_conf.request_timeout == 50);
48+
ASSERT_TRUE(cls_conf.connect_timeout == 5);
49+
50+
ASSERT_STREQ(cls_conf.topic_id.c_str(), "0***********e");
51+
ASSERT_STREQ(cls_conf.cls_region.c_str(), "a***********u");
52+
ASSERT_STREQ(cls_conf.cls_domain.c_str(), "tencentyun");
53+
ASSERT_STREQ(cls_conf.secret_id.c_str(), "A***********t");
54+
ASSERT_STREQ(cls_conf.secret_key.c_str(), "J***********x");
55+
ASSERT_STREQ(cls_conf.key_message.c_str(), "msg");
56+
ASSERT_STREQ(cls_conf.endpoint.c_str(), "a***********u.cls.tencentyun.com");
57+
4058
cls_default_ = MakeRefCounted<ClsLog>(config_list[0]);
4159
default_log_->RegisterRawSink(cls_default_);
4260
}

0 commit comments

Comments
 (0)