Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support set log level of cpp client #398

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/custom_logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion src/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
1 change: 1 addition & 0 deletions src/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading