X-Git-Url: https://git.squeep.com/?p=squeep-logger-json-console;a=blobdiff_plain;f=test%2Flib%2Flogger.js;fp=test%2Flib%2Flogger.js;h=70695a988088a56c9862e3977925a27a4ff8282d;hp=8c91b549e70ea91c01d40c6ed5ccb91d0691e798;hb=8b684b27f20657c5e0de61b4f19981937915c484;hpb=dbff0fa5f018f7a302f73250e55f761c0ccf24b1 diff --git a/test/lib/logger.js b/test/lib/logger.js index 8c91b54..70695a9 100644 --- a/test/lib/logger.js +++ b/test/lib/logger.js @@ -1,20 +1,22 @@ /* eslint-env mocha */ 'use strict'; -const assert = require('assert'); +const assert = require('node:assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const Logger = require('../../lib/logger'); -const http = require('http'); +const http = require('node:http'); +const { AsyncLocalStorage } = require('node:async_hooks'); +const asyncLocalStorage = new AsyncLocalStorage(); describe('Logger', function () { - let config, logger, scope, message; + let config, logger, commonObject, scope, message; beforeEach(function () { - config = { + config = {}; + commonObject = { nodeId: '3c100e84-9a7f-11ec-9b4e-0025905f714a', - logger: {}, }; - logger = new Logger(config); + logger = new Logger(config, commonObject); Object.keys(Logger.nullLogger).forEach((level) => sinon.stub(logger.backend, level)); scope = 'testScope'; message = 'message'; @@ -30,7 +32,10 @@ describe('Logger', function () { }); it('stubs missing levels', function () { - logger = new Logger(config); + const backend = { + debug: () => {}, + } + logger = new Logger(config, commonObject, undefined, backend); assert.strictEqual(typeof logger.info, 'function'); }); @@ -47,14 +52,14 @@ describe('Logger', function () { }); it('covers config settings', function () { - config.logger.ignoreBelowLevel = 'info'; + config.ignoreBelowLevel = 'info'; logger = new Logger(config); logger.debug(scope, message, {}); assert(logger.backend.debug.notCalled); }); it('covers config error', function () { - config.logger.ignoreBelowLevel = 'not a level'; + config.ignoreBelowLevel = 'not a level'; try { logger = new Logger(config); assert.fail('expected RangeError here'); @@ -72,7 +77,7 @@ describe('Logger', function () { it('sanitizes', function () { logger.dataSanitizers.push((data, sanitize = true) => { let unclean = false; - const credentialLength = data && data.ctx && data.ctx.parsedBody && data.ctx.parsedBody.credential && data.ctx.parsedBody.credential.length; + const credentialLength = data?.ctx?.parsedBody?.credential?.length; if (credentialLength) { unclean = true; } @@ -124,4 +129,23 @@ describe('Logger', function () { assert.deepStrictEqual(levels, expected); }); + it('logs async storage data', async function () { + logger = new Logger(config, commonObject, asyncLocalStorage); + const data = { foo: 'bar' }; + asyncLocalStorage.run(data, async () => { + logger.info(scope, message, { baz: 3 }); + }); + assert(logger.backend.info.called); + assert(logger.backend.info.args[0][0].includes('"foo":"bar"')); + }); + + it('covers no async storage', function () { + logger = new Logger(config); + const data = { foo: 'bar' }; + asyncLocalStorage.run(data, async () => { + logger.info(scope, message, { baz: 3 }); + }); + assert(logger.backend.info.called); + assert(!logger.backend.info.args[0][0].includes('"foo":"bar"')); + }); }); // Logger \ No newline at end of file