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

Add support for LOG_LEVEL env variable #148

Merged
merged 2 commits into from
Nov 22, 2023
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
12 changes: 11 additions & 1 deletion 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 All @@ -78,7 +88,7 @@ const logger = require('debugnyan')('foo', {}, { suffix: 'module', simple: false

## Release

Click on `Run Workflow` on the [release github action](https://github.com/uphold/debugnyan/actions/workflows/release.yaml)
Click `Run Workflow` on the [release github action](https://github.com/uphold/debugnyan/actions/workflows/release.yaml).

## License

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
Loading