From bf0810464592752a4c2caea6fa593547a50048d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Tue, 21 Nov 2023 12:19:25 +0000 Subject: [PATCH 1/2] Add support for `LOG_LEVEL` env variable --- README.md | 10 ++++++++++ index.js | 3 ++- test/index.test.js | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b6ed055..08f6064 100755 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/index.js b/index.js index bcaa32a..c03c80b 100755 --- a/index.js +++ b/index.js @@ -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]; diff --git a/test/index.test.js b/test/index.test.js index 1f48bc6..11a1eaa 100755 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1,5 @@ +/* eslint-disable no-process-env */ + /** * Module dependencies. */ @@ -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'); @@ -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'); From 802cb55755aafeff27fb2387ba68d7da453b0621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Tue, 21 Nov 2023 15:17:23 +0000 Subject: [PATCH 2/2] Improve release section in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08f6064..d3c6324 100755 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ You may also set the log level via the `LOG_LEVEL` environment variable. However ## 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