Permalink
Browse files

Export `bunyan.resolveLevel(NAME-OR-NUM)` to resolve a level name or …

…number to its log level number value
  • Loading branch information...
1 parent 8cfdd8b commit db836e2ddddf9bf989e94695f7b027ead017189a @trentm committed Aug 24, 2012
Showing with 53 additions and 2 deletions.
  1. +10 −1 CHANGES.md
  2. +8 −1 lib/bunyan.js
  3. +35 −0 test/other-api.test.js
View
11 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
View
9 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;
View
35 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.<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();
+});
+

0 comments on commit db836e2

Please sign in to comment.