diff --git a/lib/nsolid.js b/lib/nsolid.js index c1e3202c8c..633ae638d0 100644 --- a/lib/nsolid.js +++ b/lib/nsolid.js @@ -129,6 +129,13 @@ ObjectAssign(start, { heapSamplingStream, heapSampling: zmq.heapSampling, heapSamplingEnd: zmq.heapSamplingEnd, + logger: { + debug: (msg) => { writeLog(msg, 5) }, + info: (msg) => { writeLog(msg, 9) }, + warn: (msg) => { writeLog(msg, 13) }, + error: (msg) => { writeLog(msg, 17) }, + fatal: (msg) => { writeLog(msg, 21) }, + }, profile: zmq.profile, profileEnd: zmq.profileEnd, on, @@ -212,6 +219,11 @@ start.traceStats = assignGetters({}, { module.exports = start; +function writeLog(msg, sev) { + binding.writeLog(typeof msg === 'string' ? msg : JSONStringify(msg), sev); +} + + function stringifyStart() { const ret = {}; ObjectGetOwnPropertyNames(this).forEach((e) => { diff --git a/src/nsolid/nsolid_api.cc b/src/nsolid/nsolid_api.cc index d84e6808c2..aac95947bb 100644 --- a/src/nsolid/nsolid_api.cc +++ b/src/nsolid/nsolid_api.cc @@ -2075,6 +2075,16 @@ static void AgentId(const FunctionCallbackInfo& args) { } +static void WriteLog(const FunctionCallbackInfo& args) { + DCHECK(args[0]->IsString()); + DCHECK(args[1]->IsNumber()); + String::Utf8Value s(args.GetIsolate(), args[0]); + std::string ss = *s; + EnvList::Inst()->WriteLogLine(GetLocalEnvInst(args.GetIsolate()), + { ss, args[1].As()->Value() }); +} + + void BindingData::SlowPushClientBucket( const FunctionCallbackInfo& args) { DCHECK(args[0]->IsNumber()); @@ -2830,6 +2840,7 @@ void BindingData::Initialize(Local target, &fast_push_span_data_uint64_); SetMethod(context, target, "agentId", AgentId); + SetMethod(context, target, "writeLog", WriteLog); SetMethod(context, target, "pushSpanDataString", PushSpanDataString); SetMethod(context, target, "getEnvMetrics", GetEnvMetrics); SetMethod(context, target, "getProcessMetrics", GetProcessMetrics); @@ -2961,6 +2972,7 @@ void BindingData::RegisterExternalReferences( registry->Register(fast_push_span_data_uint64_.GetTypeInfo()); registry->Register(AgentId); + registry->Register(WriteLog); registry->Register(PushSpanDataString); registry->Register(GetEnvMetrics); registry->Register(GetProcessMetrics); diff --git a/test/addons/nsolid-log-hooks/binding.cc b/test/addons/nsolid-log-hooks/binding.cc index 4e90cf47a6..801a66f208 100644 --- a/test/addons/nsolid-log-hooks/binding.cc +++ b/test/addons/nsolid-log-hooks/binding.cc @@ -17,7 +17,7 @@ std::atomic cntr = { 0 }; // on the Environment of the main thread. So by this point the env_map_size // should equal 0. static void at_exit(void*) { - assert(cntr == 11); + assert(cntr == 66); } static void log_written(node::nsolid::SharedEnvInst, diff --git a/test/addons/nsolid-log-hooks/nsolid-log-hooks.js b/test/addons/nsolid-log-hooks/nsolid-log-hooks.js index 12fd7ec202..2606cb8846 100644 --- a/test/addons/nsolid-log-hooks/nsolid-log-hooks.js +++ b/test/addons/nsolid-log-hooks/nsolid-log-hooks.js @@ -4,14 +4,25 @@ const assert = require('assert'); const bindingPath = require.resolve(`./build/${common.buildType}/binding`); const { writeLog } = require(bindingPath); const { Worker, isMainThread } = require('worker_threads'); +const { logger } = require('nsolid'); if (!isMainThread) { writeLog('boom!'); + logger.debug('hi'); + logger.info('hi'); + logger.warn('hi'); + logger.error('hi'); + logger.fatal('hi'); setTimeout(() => {}, Math.random() * 1000); return; } writeLog('bam!'); +logger.debug('hi'); +logger.info('hi'); +logger.warn('hi'); +logger.error('hi'); +logger.fatal('hi'); for (let i = 0; i < 10; i++) { const worker = new Worker(__filename);