Export bunyan.resolveLevel(NAME-OR-NUM) to resolve a level name or number to its log level number value

This commit is contained in:
Trent Mick 2012-08-24 16:28:31 -07:00
parent 8cfdd8b76f
commit db836e2ddd
3 changed files with 53 additions and 2 deletions

View file

@ -2,7 +2,16 @@
## bunyan 0.13.3 (not yet released) ## 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 ## bunyan 0.13.2

View file

@ -156,9 +156,15 @@ var levelFromName = {
'fatal': FATAL 'fatal': FATAL
}; };
/**
* Resolve a level number, name (upper or lowercase) to a level number value.
*
* @api public
*/
function resolveLevel(nameOrNum) { function resolveLevel(nameOrNum) {
var level = (typeof (nameOrNum) === 'string' var level = (typeof (nameOrNum) === 'string'
? levelFromName[nameOrNum] ? levelFromName[nameOrNum.toLowerCase()]
: nameOrNum); : nameOrNum);
if (! (TRACE <= level && level <= FATAL)) { if (! (TRACE <= level && level <= FATAL)) {
throw new Error('invalid level: ' + nameOrNum); throw new Error('invalid level: ' + nameOrNum);
@ -1072,6 +1078,7 @@ module.exports.INFO = INFO;
module.exports.WARN = WARN; module.exports.WARN = WARN;
module.exports.ERROR = ERROR; module.exports.ERROR = ERROR;
module.exports.FATAL = FATAL; module.exports.FATAL = FATAL;
module.exports.resolveLevel = resolveLevel;
module.exports.VERSION = VERSION; module.exports.VERSION = VERSION;
module.exports.LOG_VERSION = LOG_VERSION; module.exports.LOG_VERSION = LOG_VERSION;

35
test/other-api.test.js Normal file
View file

@ -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.<LEVEL>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();
});