From ea52c79689d5ef989d22db1dfc1e1364fc4bef91 Mon Sep 17 00:00:00 2001 From: Joost de Vries Date: Tue, 6 Nov 2012 19:49:28 +0100 Subject: [PATCH 1/6] Added runtime config option --- lib/nlogger.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/nlogger.js b/lib/nlogger.js index 89e767a..398d1d8 100644 --- a/lib/nlogger.js +++ b/lib/nlogger.js @@ -24,7 +24,8 @@ */ var util = require('util'), - fs = require('fs'); + fs = require('fs'), + config = false; function padZero(number) { @@ -89,18 +90,20 @@ function getMessage(items) { } try { - var file = fs.readFileSync('./nlogger.json', 'binary'), - config = JSON.parse(file); + var file = fs.readFileSync('./nlogger.json', 'binary'); + config = JSON.parse(file); } catch(e) { util.puts(getDate() + ' WARN nlogger - Config file not found. Using default configuration.'); - config = {}; } -var defaultLogLevel = config.level && config.level['*'] || 'trace'; -var logLevels = config.level || {}; -var useColor = config.color || (config.color == 'auto' && process.env.TERM && process.env.TERM.indexOf('color') >= 0); +exports.logger = function(module,_config) { + + config = (_config && (typeof _config == 'object')) ? _config : (config ? config : {}); + + var defaultLogLevel = config.level && config.level['*'] || 'trace'; + var logLevels = config.level || {}; + var useColor = config.color || (config.color == 'auto' && process.env.TERM && process.env.TERM.indexOf('color') >= 0); -exports.logger = function(module) { var methods = { 'trace': { 'color': 32, 'priority': 1 }, 'debug': { 'color': 34, 'priority': 2 }, From 86a6e4ef404e2042e63a2a5b450df729ba4d5523 Mon Sep 17 00:00:00 2001 From: Joost de Vries Date: Tue, 6 Nov 2012 19:53:28 +0100 Subject: [PATCH 2/6] Warning removed --- lib/nlogger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nlogger.js b/lib/nlogger.js index 398d1d8..49ddcd1 100644 --- a/lib/nlogger.js +++ b/lib/nlogger.js @@ -93,7 +93,7 @@ try { var file = fs.readFileSync('./nlogger.json', 'binary'); config = JSON.parse(file); } catch(e) { - util.puts(getDate() + ' WARN nlogger - Config file not found. Using default configuration.'); + } exports.logger = function(module,_config) { From 9b12478da6004af1f8db62654da490eebb1c4e51 Mon Sep 17 00:00:00 2001 From: Joost de Vries Date: Tue, 6 Nov 2012 20:00:57 +0100 Subject: [PATCH 3/6] Added examples --- examples/app.js | 5 ++++- examples/my-modules/runtime-config.js | 4 ++++ examples/my-modules/second.js | 3 --- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 examples/my-modules/runtime-config.js delete mode 100644 examples/my-modules/second.js diff --git a/examples/app.js b/examples/app.js index 9c39778..df47b03 100644 --- a/examples/app.js +++ b/examples/app.js @@ -1,6 +1,9 @@ var logger = require('../lib/nlogger').logger(module); var first = require('./my-modules/first'); -var second = require('./my-modules/second'); +/** + * Example with runtime config - see module code + */ +var second = require('./my-modules/runtime-config'); var third = require('./my-modules/third'); setTimeout(function() { diff --git a/examples/my-modules/runtime-config.js b/examples/my-modules/runtime-config.js new file mode 100644 index 0000000..02a959a --- /dev/null +++ b/examples/my-modules/runtime-config.js @@ -0,0 +1,4 @@ +var logger = require('../../lib/nlogger').logger('my-custom-name',{level:{'*':'warn'}}); + +logger.info('Because of the runtime cfg, this will be ignored.'); +logger.warn('But this is visible. Notice the custom name too :-)'); diff --git a/examples/my-modules/second.js b/examples/my-modules/second.js deleted file mode 100644 index d1d8bff..0000000 --- a/examples/my-modules/second.js +++ /dev/null @@ -1,3 +0,0 @@ -var logger = require('../../lib/nlogger').logger('my-custom-name'); - -logger.info('Message from second module with defined module name'); From f4a7d15ca22ca1cd46b1c7dfcffae0c8d3bb7e94 Mon Sep 17 00:00:00 2001 From: Joost de Vries Date: Tue, 6 Nov 2012 20:26:32 +0100 Subject: [PATCH 4/6] Added stderr support --- lib/nlogger.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/nlogger.js b/lib/nlogger.js index 49ddcd1..b396b96 100644 --- a/lib/nlogger.js +++ b/lib/nlogger.js @@ -105,11 +105,11 @@ exports.logger = function(module,_config) { var useColor = config.color || (config.color == 'auto' && process.env.TERM && process.env.TERM.indexOf('color') >= 0); var methods = { - 'trace': { 'color': 32, 'priority': 1 }, - 'debug': { 'color': 34, 'priority': 2 }, - 'info': { 'color': 30, 'priority': 3 }, - 'warn': { 'color': 35, 'priority': 4 }, - 'error': { 'color': 31, 'priority': 5 } + 'trace': { 'color': 32, 'priority': 1, 'error' : 0 }, + 'debug': { 'color': 34, 'priority': 2, 'error' : 0 }, + 'info': { 'color': 30, 'priority': 3, 'error' : 0 }, + 'warn': { 'color': 35, 'priority': 4, 'error' : 1 }, + 'error': { 'color': 31, 'priority': 5, 'error' : 1 } }; var logLevel = logLevels[getClass(module)] || defaultLogLevel; @@ -129,7 +129,12 @@ exports.logger = function(module,_config) { } else { logger[level] = function(msg) { if (methods[level].priority >= priority) { - util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments)); + if(methods[level].error) { + util.error(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments)); + } + else { + util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments)); + } } }; } From 02171ae2148905aea9951361ff40f77f1ccd3271 Mon Sep 17 00:00:00 2001 From: Joost de Vries Date: Tue, 6 Nov 2012 20:28:16 +0100 Subject: [PATCH 5/6] now uses util.format instead of custom function --- lib/nlogger.js | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/lib/nlogger.js b/lib/nlogger.js index b396b96..5cb4d68 100644 --- a/lib/nlogger.js +++ b/lib/nlogger.js @@ -77,18 +77,6 @@ function getClass(module) { } } -function getMessage(items) { - var msg = [], i; - for (i = 0; i < items.length; i++) { - if (typeof items[i] == 'string') { - msg.push(items[i]); - } else { - msg.push(util.inspect(items[i], false, 10)); - } - } - return msg.join(''); -} - try { var file = fs.readFileSync('./nlogger.json', 'binary'); config = JSON.parse(file); @@ -123,17 +111,17 @@ exports.logger = function(module,_config) { if (useColor) { logger[level] = function(msg) { if (methods[level].priority >= priority) { - util.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments) + '\x1B[0m'); + util.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format(arguments) + '\x1B[0m'); } }; } else { logger[level] = function(msg) { if (methods[level].priority >= priority) { if(methods[level].error) { - util.error(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments)); + util.error(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format(arguments)); } else { - util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments)); + util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format(arguments)); } } }; From 7ae63656f69d47c2fa73a9bf464620d99c98ec5b Mon Sep 17 00:00:00 2001 From: Joost de Vries Date: Tue, 6 Nov 2012 20:35:19 +0100 Subject: [PATCH 6/6] Fixed error and added example --- examples/my-modules/runtime-config.js | 1 + lib/nlogger.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/my-modules/runtime-config.js b/examples/my-modules/runtime-config.js index 02a959a..47b16a6 100644 --- a/examples/my-modules/runtime-config.js +++ b/examples/my-modules/runtime-config.js @@ -2,3 +2,4 @@ var logger = require('../../lib/nlogger').logger('my-custom-name',{level:{'*':'w logger.info('Because of the runtime cfg, this will be ignored.'); logger.warn('But this is visible. Notice the custom name too :-)'); +logger.error('And here is a formated string with a number %d and an object %s',5,require('util').inspect({a:1})); diff --git a/lib/nlogger.js b/lib/nlogger.js index 5cb4d68..c7e414d 100644 --- a/lib/nlogger.js +++ b/lib/nlogger.js @@ -118,10 +118,10 @@ exports.logger = function(module,_config) { logger[level] = function(msg) { if (methods[level].priority >= priority) { if(methods[level].error) { - util.error(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format(arguments)); + util.error(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format.apply(util,arguments)); } else { - util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format(arguments)); + util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + util.format.apply(util,arguments)); } } };