Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make root logger global so it's shared amongst versions #7

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading