Skip to content

Commit

Permalink
test: fixed logger_test
Browse files Browse the repository at this point in the history
  • Loading branch information
abhilash-sivan committed Dec 16, 2024
1 parent 158cbe4 commit bd47728
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/collector/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -71,6 +70,7 @@ exports.init = function init(config, isReInit) {
parentLogger = pino(
{
...parentLogger.levels,
level: parentLogger.level || 'info',
base: parentLogger.bindings()
},
multiStream
Expand Down
45 changes: 22 additions & 23 deletions packages/collector/test/logger_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const logLevel = {
10: 'trace'
};

describe.only('logger', () => {
describe('logger', () => {
beforeEach(resetEnv);
afterEach(resetEnv);

Expand All @@ -37,29 +37,34 @@ 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', () => {
const logger = pino({ name: 'myParentLogger' });
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', () => {
Expand All @@ -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]);
Expand Down Expand Up @@ -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' });
Expand All @@ -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', () => {
Expand All @@ -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 });
Expand All @@ -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;
}

0 comments on commit bd47728

Please sign in to comment.