diff --git a/README.md b/README.md index b6ed055..e8fb42b 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 logger1 = 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');