From bd47728cc9487be01eb039c7e091eb18630a8c89 Mon Sep 17 00:00:00 2001 From: Abhilash Date: Mon, 16 Dec 2024 19:23:23 +0530 Subject: [PATCH] test: fixed logger_test --- packages/collector/src/logger.js | 2 +- packages/collector/test/logger_test.js | 45 +++++++++++++------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/collector/src/logger.js b/packages/collector/src/logger.js index b9d21de63..3cff3c5f4 100644 --- a/packages/collector/src/logger.js +++ b/packages/collector/src/logger.js @@ -44,7 +44,6 @@ exports.init = function init(config, isReInit) { parentLogger = config.logger; } else { // No custom logger has been provided; create a new pino logger as the parent logger for all loggers - parentLogger = pino({ name: '@instana/collector', base: { @@ -71,6 +70,7 @@ exports.init = function init(config, isReInit) { parentLogger = pino( { ...parentLogger.levels, + level: parentLogger.level || 'info', base: parentLogger.bindings() }, multiStream diff --git a/packages/collector/test/logger_test.js b/packages/collector/test/logger_test.js index 4025e658f..f0492619e 100644 --- a/packages/collector/test/logger_test.js +++ b/packages/collector/test/logger_test.js @@ -19,7 +19,7 @@ const logLevel = { 10: 'trace' }; -describe.only('logger', () => { +describe('logger', () => { beforeEach(resetEnv); afterEach(resetEnv); @@ -37,20 +37,23 @@ describe.only('logger', () => { it('should return a child logger if requested', () => { log.init({}); const logger = log.getLogger('childName'); + const metadata = getLoggerMetadata(logger); + expect(isPinoLogger(logger)).to.be.true; - // expect(logger.logger.fields).to.have.property('module'); - // expect(logger.logger.fields.module).to.equal('childName'); + expect(metadata).to.have.property('module'); + expect(metadata.module).to.equal('childName'); }); it('should use the parent logger if defined', () => { const logger = pino({ name: 'myParentLogger' }); log.init({ logger }); const childLogger = log.getLogger('childName'); + const metadata = getLoggerMetadata(childLogger); expect(isPinoLogger(logger)).to.be.true; - // expect(childLogger.logger.fields).to.have.property('module'); - // expect(childLogger.logger.fields.module).to.equal('childName'); + expect(metadata).to.have.property('module'); + expect(metadata.module).to.equal('childName'); }); it('should add child logger to defined parent', () => { @@ -58,8 +61,10 @@ describe.only('logger', () => { log.init({ logger }); const childLogger = log.getLogger('childName'); - // expect(parentLogger.logger.fields).to.have.property('module'); - // expect(parentLogger.logger.fields.module).to.equal('childName'); + const childtMetaData = getLoggerMetadata(childLogger); + + expect(childtMetaData).to.have.property('module'); + expect(childtMetaData?.module).to.equal('childName'); }); it('should use default log level if not defined', () => { @@ -70,7 +75,7 @@ describe.only('logger', () => { }); it('should use defined log level', () => { - log.init({ logger: pino({ name: 'myParentLogger', level: 'error' }) }); + log.init({ logger: pino({ name: 'myParentLogger', level: 50 }) }); const logger = log.getLogger('childName'); expect(logger.level).to.equal(logLevel[50]); @@ -139,11 +144,9 @@ describe.only('logger', () => { expect(isPinoLogger(logger)).to.be.true; - const data = logger; // [Symbol.for('pino.chindings')]; - const metadata = getLoggerMetadata(logger); - // expect(metadata.name).to.equal('@instana/collector'); + expect(metadata.name).to.equal('@instana/collector'); const originalLogger = logger; const logger2 = pino({ name: 'new-logger' }); @@ -153,7 +156,9 @@ describe.only('logger', () => { expect(isPinoLogger(logger)).to.be.true; expect(logger === originalLogger).to.not.be.true; - // expect(logger.name).to.equal('new-logger'); + const metadata2 = getLoggerMetadata(logger2); + + expect(metadata2.name).to.equal('new-logger'); }); it('should not choke on re-initialization when there is no reInit callback', () => { @@ -162,7 +167,9 @@ describe.only('logger', () => { // first getLogger call should yield the default pino logger expect(isPinoLogger(logger)).to.be.true; - // expect(logger.logger.fields.name).to.equal('@instana/collector'); + + const metadata = getLoggerMetadata(logger); + expect(metadata.name).to.equal('@instana/collector'); const logger2 = pino({ name: 'new-logger' }); log.init({ logger: logger2 }); @@ -187,15 +194,7 @@ function isPinoLogger(_logger) { * function to extract logger metadata from Pino's internal `chindings` symbol. */ function getLoggerMetadata(logger) { - // if (logger.fields) - const chindings = logger[Symbol.for('pino.chindings')]; + const metadata = logger.bindings() || {}; - // console.log(Object.values(logger)); - - // console.log('val:', chindings); - if (chindings) { - const metadata = JSON.parse(chindings); - return metadata; - } - return {}; + return metadata; }