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 7631664
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 35 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A logging library that combines the simplicity and convenience of [debug](https:
## Installation

```bash
npm install @uphold/debino
npm install @uphold/debino pino
```

## Usage
Expand Down Expand Up @@ -74,7 +74,8 @@ You may also set the log level via the `LOG_LEVEL` environment variable. However
Every call to `debino` creates a child logger based on a root logger. The default root logger is an instance returned by `pino()`, without any options. You may set your own root logger by calling `setRootLogger()`:

```js
import { debino, pino, setRootLogger } from '@uphold/debino';
import { debino, setRootLogger } from '@uphold/debino';
import pino from 'pino';

// Call this as soon as possible in your application.
setRootLogger(pino({ redact: ['foo'] }));
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"peerDependencies": {
"pino": ">= 9.0.0"
},
"devDependencies": {
Expand All @@ -43,7 +43,7 @@
"eslint-plugin-sort-imports-requires": "^2.0.0",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"globals": "^15.14.0",
"pino": "^9.0.0",
"pino": "^9.6.0",
"prettier": "^3.4.2",
"release-it": "^17.10.0",
"vitest": "^2.1.8"
Expand Down
20 changes: 10 additions & 10 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,15 +72,17 @@ 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.
*/

module.exports.debino = debino;
module.exports.pino = pino;
module.exports.setRootLogger = setRootLogger;
27 changes: 12 additions & 15 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { debino, pino, setRootLogger } from '.';
import { debino, setRootLogger } from '.';
import debug from 'debug';
import pino from 'pino';

/**
* Tests for `debino()`.
*/

beforeEach(() => {
global[Symbol.for('debino')].loggers.clear();
});

describe('debino', () => {
beforeEach(() => {
delete process.env.LOG_LEVEL;
Expand Down Expand Up @@ -102,6 +107,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 +126,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);
});
});
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger, ChildLoggerOptions, pino } from "pino";
import { Logger, ChildLoggerOptions } from "pino";

declare function debino(namespace: string, options?: { prefix?: string, suffix?: string } & ChildLoggerOptions): Logger

declare function setRootLogger(logger: Logger)

export { debino, pino, setRootLogger }
export { debino, setRootLogger }

0 comments on commit 7631664

Please sign in to comment.