From db836e2ddddf9bf989e94695f7b027ead017189a Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 24 Aug 2012 16:28:31 -0700 Subject: [PATCH] Export `bunyan.resolveLevel(NAME-OR-NUM)` to resolve a level name or number to its log level number value --- CHANGES.md | 11 ++++++++++- lib/bunyan.js | 9 ++++++++- test/other-api.test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/other-api.test.js diff --git a/CHANGES.md b/CHANGES.md index 496a917..34b5be5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,16 @@ ## bunyan 0.13.3 (not yet released) -(nothing yet) +- Export `bunyan.resolveLevel(NAME-OR-NUM)` to resolve a level name or number + to its log level number value: + + > bunyan.resolveLevel('INFO') + 30 + > bunyan.resolveLevel('debug') + 20 + + A side-effect of this change is that the uppercase level name is now allowed + in the logger constructor. ## bunyan 0.13.2 diff --git a/lib/bunyan.js b/lib/bunyan.js index ed464ba..95cbdb2 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -156,9 +156,15 @@ var levelFromName = { 'fatal': FATAL }; + +/** + * Resolve a level number, name (upper or lowercase) to a level number value. + * + * @api public + */ function resolveLevel(nameOrNum) { var level = (typeof (nameOrNum) === 'string' - ? levelFromName[nameOrNum] + ? levelFromName[nameOrNum.toLowerCase()] : nameOrNum); if (! (TRACE <= level && level <= FATAL)) { throw new Error('invalid level: ' + nameOrNum); @@ -1072,6 +1078,7 @@ module.exports.INFO = INFO; module.exports.WARN = WARN; module.exports.ERROR = ERROR; module.exports.FATAL = FATAL; +module.exports.resolveLevel = resolveLevel; module.exports.VERSION = VERSION; module.exports.LOG_VERSION = LOG_VERSION; diff --git a/test/other-api.test.js b/test/other-api.test.js new file mode 100644 index 0000000..25bee68 --- /dev/null +++ b/test/other-api.test.js @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2012 Trent Mick. All rights reserved. + * + * Test other parts of the exported API. + */ + +var test = require('tap').test; +var bunyan = require('../lib/bunyan'); + +test('bunyan.s', function (t) { + t.ok(bunyan.TRACE, 'TRACE'); + t.ok(bunyan.DEBUG, 'DEBUG'); + t.ok(bunyan.INFO, 'INFO'); + t.ok(bunyan.WARN, 'WARN'); + t.ok(bunyan.ERROR, 'ERROR'); + t.ok(bunyan.FATAL, 'FATAL'); + t.end(); +}); + +test('bunyan.resolveLevel()', function (t) { + t.equal(bunyan.resolveLevel('trace'), bunyan.TRACE, 'TRACE'); + t.equal(bunyan.resolveLevel('TRACE'), bunyan.TRACE, 'TRACE'); + t.equal(bunyan.resolveLevel('debug'), bunyan.DEBUG, 'DEBUG'); + t.equal(bunyan.resolveLevel('DEBUG'), bunyan.DEBUG, 'DEBUG'); + t.equal(bunyan.resolveLevel('info'), bunyan.INFO, 'INFO'); + t.equal(bunyan.resolveLevel('INFO'), bunyan.INFO, 'INFO'); + t.equal(bunyan.resolveLevel('warn'), bunyan.WARN, 'WARN'); + t.equal(bunyan.resolveLevel('WARN'), bunyan.WARN, 'WARN'); + t.equal(bunyan.resolveLevel('error'), bunyan.ERROR, 'ERROR'); + t.equal(bunyan.resolveLevel('ERROR'), bunyan.ERROR, 'ERROR'); + t.equal(bunyan.resolveLevel('fatal'), bunyan.FATAL, 'FATAL'); + t.equal(bunyan.resolveLevel('FATAL'), bunyan.FATAL, 'FATAL'); + t.end(); +}); +