This is an automated email from the ASF dual-hosted git repository.
baodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-node.git
The following commit(s) were added to refs/heads/master by this push:
new 511f707 Support set log level of cpp client (#398)
511f707 is described below
commit 511f707f24f4c9dbafa5164a77dbc53bbc4b6bc0
Author: Baodi Shi <[email protected]>
AuthorDate: Tue Oct 15 15:20:55 2024 +0800
Support set log level of cpp client (#398)
---
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 7f2e426..60b2310 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 29c1967..7c2ae81 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 e3cbb72..a9df576 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<Client>(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<pulsar_logger_level_t>(logLevelInt);
+ }
+ pulsar_logger_t logger;
+ logger.ctx = &this->logLevel;
+ logger.is_enabled = [](pulsar_logger_level_t level, void *ctx) {
+ auto *logLevel = static_cast<pulsar_logger_level_t *>(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 8d2ba03..ee81bb5 100644
--- a/src/Client.h
+++ b/src/Client.h
@@ -53,6 +53,7 @@ class Client : public Napi::ObjectWrap<Client> {
static Napi::FunctionReference constructor;
std::shared_ptr<pulsar_client_t> cClient;
std::shared_ptr<pulsar_client_configuration_t> 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);