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

Set the default child logger level to inherit from the root logger #10

Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ const logger = debino('root')('foo', { level: 'info' });
```

You may also set the log level via the `LOG_LEVEL` environment variable. However, the `level` option will always take precedence over it.
If both `level` and `LOG_LEVEL` are not set, it will use the value from
the root logger (See the [Root Logger](#root-logger) section below for more information).

### Root logger

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()`:
Every call to `debino` creates a child logger based on a root logger. The default root logger is an instance returned by `pino({ level: 'debug' })`. You may set your own root logger by calling `setRootLogger()`:

```js
import { debino, pino, setRootLogger } from '@uphold/debino';
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const debino = (namespace, { prefix = 'sub', suffix = 'component', ...options }

// Ensure the root logger is set.
if (!rootLogger) {
rootLogger = global[globalSymbol].rootLogger = pino();
rootLogger = global[globalSymbol].rootLogger = pino({ level: 'debug' });
}

// Create the logger for this namespace if it doesn't exist.
Expand All @@ -68,7 +68,7 @@ const debino = (namespace, { prefix = 'sub', suffix = 'component', ...options }

// Set the log level based on the debug namespace.
if (debug.enabled(namespace)) {
childLogger.level = options.level ?? process.env.LOG_LEVEL ?? 'debug';
childLogger.level = options.level ?? process.env.LOG_LEVEL ?? rootLogger.level;
} else {
childLogger.level = 'silent';
}
Expand Down
9 changes: 9 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ describe('debino', () => {
expect(logger.level).toEqual('warn');
});

it('should be on the specified level on the root logger if `DEBUG` matches logger name', () => {
debug.enable('abc');
setRootLogger(pino({ level: 'warn' }));

const logger = debino('abc');

expect(logger.level).toEqual('warn');
});

it('should support multiple components separated by colons', () => {
const logger = debino('foo:bar:biz:qux');
const bindings = logger.bindings();
Expand Down
Loading