From de046691b5d8c87af33d6046f6dc3d63cd9fc51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Mon, 23 Dec 2024 10:56:31 +0000 Subject: [PATCH] Make root logger global so it's shared amongst versions --- package-lock.json | 6 +++--- src/index.js | 19 ++++++++++--------- src/index.test.js | 24 ++++++++++-------------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 502eceb..6b50b20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "@uphold/debino", "version": "0.3.0", "license": "MIT", + "dependencies": { + "pino": ">= 9.0.0" + }, "devDependencies": { "@eslint/js": "^9.17.0", "@vitest/coverage-v8": "^2.1.8", @@ -22,9 +25,6 @@ "prettier": "^3.4.2", "release-it": "^17.10.0", "vitest": "^2.1.8" - }, - "peerDependencies": { - "pino": ">= 9.0.0" } }, "node_modules/@ampproject/remapping": { diff --git a/src/index.js b/src/index.js index 4864c09..25824b8 100644 --- a/src/index.js +++ b/src/index.js @@ -9,8 +9,7 @@ const pino = require('pino'); * Variables */ -let rootLogger; -const loggersSymbol = Symbol('debino.loggers'); +const globalSymbol = Symbol.for('debino'); /** * Create child logger bindings based on a given `namespace`, `prefix` and `suffix`. @@ -39,12 +38,11 @@ const setRootLogger = logger => { throw new Error('The logger instance must not have a name binding configured'); } - // Ensure loggers cache map is set on the root logger. - if (!logger[loggersSymbol]) { - logger[loggersSymbol] = new Map(); + if (global[globalSymbol].loggers.size > 0) { + throw new Error('The root logger must be set before creating any child logger'); } - rootLogger = logger; + global[globalSymbol].rootLogger = logger; }; /** @@ -52,7 +50,7 @@ const setRootLogger = logger => { */ const debino = (namespace, { prefix = 'sub', suffix = 'component', ...options } = {}) => { - const loggers = rootLogger[loggersSymbol]; + const { loggers, rootLogger } = global[globalSymbol]; let childLogger = loggers.get(namespace); // Create the logger for this namespace if it doesn't exist. @@ -74,10 +72,13 @@ const debino = (namespace, { prefix = 'sub', suffix = 'component', ...options } }; /** - * Configure the default root logger. + * Configure the default root logger and initialize loggers cache. */ -setRootLogger(pino()); +global[globalSymbol] = { + loggers: new Map(), + rootLogger: pino() +}; /** * Exports. diff --git a/src/index.test.js b/src/index.test.js index 0eaa9ab..5ccd4b6 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -10,6 +10,10 @@ import debug from 'debug'; * Tests for `debino()`. */ +beforeEach(() => { + global[Symbol.for('debino')].loggers.clear(); +}); + describe('debino', () => { beforeEach(() => { delete process.env.LOG_LEVEL; @@ -102,6 +106,12 @@ describe('setRootLogger', () => { ); }); + it('should throw an error if called after a child logger has been created', () => { + debino('foo'); + + expect(() => setRootLogger(pino())).toThrow('The root logger must be set before creating any child logger'); + }); + it('should store the logger as the root one', () => { const rootLogger = pino({ base: { foo: 'bar' } }); @@ -115,18 +125,4 @@ describe('setRootLogger', () => { expect(logger.bindings().name).toBe('foo'); expect(logger.bindings().foo).toBe('bar'); }); - - it('should not overwrite loggers cache if already set', () => { - const rootLogger = pino(); - - setRootLogger(rootLogger); - - const child1 = debino('foo'); - - setRootLogger(rootLogger); - - const child2 = debino('foo'); - - expect(child1).toBe(child2); - }); });