Skip to content

Commit

Permalink
Add support for LOG_LEVEL env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor authored and jspranger committed Nov 22, 2023
1 parent 2fbfce4 commit b4a6dcf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ When creating a _child_ logger you may also override the default `simple` behavi
const logger = require('debugnyan')('foo', {}, { suffix: 'module', simple: false });
```

### Log level

The `level` bunyan option is respected if the logger output is active.

```js
const logger = require('debugnyan')('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.

## Tests

```
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ module.exports = function debugnyan(name, options, { prefix = 'sub', simple = tr
}

if (debug.enabled(name)) {
loggers[name].level(options?.level ?? bunyan.DEBUG);
// eslint-disable-next-line no-process-env
loggers[name].level(options?.level ?? process.env.LOG_LEVEL ?? bunyan.DEBUG);
}

return loggers[name];
Expand Down
18 changes: 17 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-process-env */

/**
* Module dependencies.
*/
Expand All @@ -7,6 +9,10 @@ const debug = require('debug');
const debugnyan = require('..');

describe('debugnyan', () => {
beforeEach(() => {
delete process.env.LOG_LEVEL;
});

it('should return an instance of a bunyan logger', () => {
const logger = debugnyan('foo');

Expand Down Expand Up @@ -48,14 +54,24 @@ describe('debugnyan', () => {
expect(logger.level()).toEqual(Logger.DEBUG);
});

it('should be on the specified level if `DEBUG` matches logger name', () => {
it('should be on the specified level via `options.level` if `DEBUG` matches logger name', () => {
debug.enable('abc');
process.env.LOG_LEVEL = 'info';

const logger = debugnyan('abc', { level: 'warn' });

expect(logger.level()).toEqual(Logger.WARN);
});

it('should be on the specified level via `process.env.LOG_LEVEL` if `DEBUG` matches logger name', () => {
debug.enable('abc');
process.env.LOG_LEVEL = 'warn';

const logger = debugnyan('abc');

expect(logger.level()).toEqual(Logger.WARN);
});

describe('namespacing', () => {
it('should support multiple components separated by colons', () => {
const logger = debugnyan('foo:bar:biz:qux');
Expand Down

0 comments on commit b4a6dcf

Please sign in to comment.