From a5875efae14de659218e4523fed9ffa34921eb69 Mon Sep 17 00:00:00 2001 From: Baodi Shi Date: Tue, 15 Oct 2024 11:12:06 +0800 Subject: [PATCH] Support set log level of cpp client --- examples/custom_logger.js | 8 ++++---- index.d.ts | 1 + src/Client.cc | 14 +++++++++++++- src/Client.h | 1 + 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/custom_logger.js b/examples/custom_logger.js index 7f2e4265..60b23104 100644 --- a/examples/custom_logger.js +++ b/examples/custom_logger.js @@ -24,10 +24,10 @@ const Pulsar = require('..'); const client = new Pulsar.Client({ serviceUrl: 'pulsar://localhost:6650', operationTimeoutSeconds: 30, - }); - - Pulsar.Client.setLogHandler((level, file, line, message) => { - console.log('[%s][%s:%d] %s', Pulsar.LogLevel.toString(level), file, line, message); + log: (level, file, line, message) => { + console.log('[%s][%s:%d] %s', Pulsar.LogLevel.toString(level), file, line, message); + }, + logLevel: Pulsar.LogLevel.DEBUG, }); // Create a producer diff --git a/index.d.ts b/index.d.ts index 29c1967d..7c2ae814 100644 --- a/index.d.ts +++ b/index.d.ts @@ -32,6 +32,7 @@ export interface ClientConfig { statsIntervalInSeconds?: number; listenerName?: string; log?: (level: LogLevel, file: string, line: number, message: string) => void; + logLevel?: LogLevel; } export class Client { diff --git a/src/Client.cc b/src/Client.cc index e3cbb729..a9df5763 100644 --- a/src/Client.cc +++ b/src/Client.cc @@ -40,6 +40,7 @@ static const std::string CFG_TLS_VALIDATE_HOSTNAME = "tlsValidateHostname"; static const std::string CFG_TLS_ALLOW_INSECURE = "tlsAllowInsecureConnection"; static const std::string CFG_STATS_INTERVAL = "statsIntervalInSeconds"; static const std::string CFG_LOG = "log"; +static const std::string CFG_LOG_LEVEL = "logLevel"; static const std::string CFG_LISTENER_NAME = "listenerName"; LogCallback *Client::logCallback = nullptr; @@ -107,7 +108,18 @@ Client::Client(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) pulsar_client_configuration_free); // The logger can only be set once per process, so we will take control of it - pulsar_client_configuration_set_logger(cClientConfig.get(), &LogMessage, nullptr); + if (clientConfig.Has(CFG_LOG_LEVEL) && clientConfig.Get(CFG_LOG_LEVEL).IsNumber()) { + int32_t logLevelInt = clientConfig.Get(CFG_LOG_LEVEL).ToNumber().Int32Value(); + this->logLevel = static_cast(logLevelInt); + } + pulsar_logger_t logger; + logger.ctx = &this->logLevel; + logger.is_enabled = [](pulsar_logger_level_t level, void *ctx) { + auto *logLevel = static_cast(ctx); + return level >= *logLevel; + }; + logger.log = &LogMessage; + pulsar_client_configuration_set_logger_t(cClientConfig.get(), logger); // log config option should be deprecated in favour of static setLogHandler method if (clientConfig.Has(CFG_LOG) && clientConfig.Get(CFG_LOG).IsFunction()) { diff --git a/src/Client.h b/src/Client.h index 8d2ba031..ee81bb5a 100644 --- a/src/Client.h +++ b/src/Client.h @@ -53,6 +53,7 @@ class Client : public Napi::ObjectWrap { static Napi::FunctionReference constructor; std::shared_ptr cClient; std::shared_ptr cClientConfig; + pulsar_logger_level_t logLevel = pulsar_logger_level_t::pulsar_INFO; Napi::Value CreateProducer(const Napi::CallbackInfo &info); Napi::Value Subscribe(const Napi::CallbackInfo &info);