From 74a2a1c57d9b73e7630a9ceb58ad886011c939f6 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 13 Nov 2014 23:15:21 -0800 Subject: [PATCH] allow Logger level values outside TRACE..FATAL --- CHANGES.md | 6 +++- lib/bunyan.js | 3 -- test/level.test.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 test/level.test.js diff --git a/CHANGES.md b/CHANGES.md index b972290..e1a4cac 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,11 @@ Known issues: ## bunyan 1.2.2 (not yet released) -(nothing yet) +- Drop the guard that a bunyan Logger level must be between TRACE (10) + and FATAL (60), inclusive. This allows a trick of setting the level + to `FATAL + 1` to turn logging off. While the standard named log levels are + the golden path, then intention was not to get in the way of using + other level numbers. ## bunyan 1.2.1 diff --git a/lib/bunyan.js b/lib/bunyan.js index d60ca1d..c4a18ff 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -205,9 +205,6 @@ function resolveLevel(nameOrNum) { var level = (typeof (nameOrNum) === 'string' ? levelFromName[nameOrNum.toLowerCase()] : nameOrNum); - if (! (TRACE <= level && level <= FATAL)) { - throw new Error('invalid level: ' + nameOrNum); - } return level; } diff --git a/test/level.test.js b/test/level.test.js new file mode 100644 index 0000000..7e6a33f --- /dev/null +++ b/test/level.test.js @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2014 Trent Mick. All rights reserved. + * + * Test the `log.level(...)`. + */ + +var util = require('util'), + format = util.format, + inspect = util.inspect; +var p = console.log; + +var bunyan = require('../lib/bunyan'); + +// node-tap API +if (require.cache[__dirname + '/tap4nodeunit.js']) + delete require.cache[__dirname + '/tap4nodeunit.js']; +var tap4nodeunit = require('./tap4nodeunit.js'); +var after = tap4nodeunit.after; +var before = tap4nodeunit.before; +var test = tap4nodeunit.test; + + +// ---- test boolean `log.()` calls + +var log1 = bunyan.createLogger({ + name: 'log1', + streams: [ + { + path: __dirname + '/level.test.log1.log', + level: 'info' + } + ] +}); + + +test('log.level() -> level num', function (t) { + t.equal(log1.level(), bunyan.INFO); + t.end(); +}); + +test('log.level()', function (t) { + log1.level(bunyan.DEBUG); + t.equal(log1.level(), bunyan.DEBUG); + t.end(); +}); + +test('log.level()', function (t) { + log1.level(10); + t.equal(log1.level(), bunyan.TRACE); + t.end(); +}); + +test('log.level()', function (t) { + log1.level('error'); + t.equal(log1.level(), bunyan.ERROR); + t.end(); +}); + +// A trick to turn logging off. +// See . +test('log.level(FATAL + 1)', function (t) { + log1.level(bunyan.FATAL + 1); + t.equal(log1.level(), bunyan.FATAL + 1); + t.end(); +}); + +test('log.level()', function (t) { + log1.level(0); + t.equal(log1.level(), 0); + log1.level(Number.MAX_VALUE); + t.equal(log1.level(), Number.MAX_VALUE); + t.end(); +});