Skip to content

Commit

Permalink
Make root logger global so it's shared amongst versions
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Dec 23, 2024
1 parent c28ab09 commit 528d548
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -39,20 +38,19 @@ 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;
};

/**
* Create a logger based on a given `namespace` and `options`.
*/

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.
Expand All @@ -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.
Expand Down
24 changes: 10 additions & 14 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' } });

Expand All @@ -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);
});
});

0 comments on commit 528d548

Please sign in to comment.