From b052324d362da4739dbcd4226b27202160405b16 Mon Sep 17 00:00:00 2001 From: hbeeken Date: Thu, 29 Jan 2015 13:48:50 +0000 Subject: [PATCH] Adding logging & metric recording configuration via settings --- red/log.js | 55 +++++++++++++++++++++++-- red/nodes/Node.js | 2 +- red/red.js | 1 + settings.js | 14 ++++++- test/nodes/core/core/58-debug_spec.js | 4 +- test/nodes/core/parsers/70-HTML_spec.js | 2 +- test/nodes/core/parsers/70-JSON_spec.js | 4 +- test/nodes/core/parsers/70-XML_spec.js | 2 +- test/red/nodes/Node_spec.js | 4 +- 9 files changed, 75 insertions(+), 13 deletions(-) diff --git a/red/log.js b/red/log.js index e122574a4..ec00bf380 100644 --- a/red/log.js +++ b/red/log.js @@ -17,17 +17,66 @@ var util = require("util"); var EventEmitter = require("events").EventEmitter; +var logLevel; +var metricsOn; + +var levels = { + "fatal" : 10, + "error" : 20, + "warn" : 30, + "info" : 40, + "debug" : 50, + "trace" : 60 +} + var logHandlers = []; var ConsoleLogHandler = new EventEmitter(); ConsoleLogHandler.on("log",function(msg) { - if (msg.level !== 'metric') { + if ((msg.level === 'metric')) { + if (metricsOn) { + util.log("["+msg.level+"] ["+msg.event+":"+ msg.nodeid+":"+ msg.msguuid+"]"+metrics); + } + } else if (shouldReportMessage(msg.level)) { util.log("["+msg.level+"] ["+msg.type+":"+(msg.name||msg.id)+"] "+msg.msg); - } - + } }); +function shouldReportMessage(msglevel) { + if (((msglevel === 'fatal') && (logLevel === 10)) || + ((msglevel === 'error') && (logLevel >= 20)) || + ((msglevel === 'warn') && (logLevel >= 30)) || + ((msglevel === 'info') && (logLevel >= 40)) || + ((msglevel === 'debug') && (logLevel >= 50)) || + ((msglevel === 'trace') && (logLevel === 60))) { + return true; + } + return false; +} + + var log = module.exports = { + + init: function(settings) { + if (settings.logLevel) { + var levelNames = Object.keys(levels); + levelNames.forEach(function(levelName) { + if (levelName === settings.logLevel) { + logLevel =levels[levelName]; + } + }); + } + if (!logLevel) { + // handles case if someone has put in garbage for the log level + logLevel = 40; + } + if (settings.metricsOn) { + metricsOn = settings.metricsOn + } else { + metricsOn = false; + } + }, + addHandler: function(func) { logHandlers.push(func); }, diff --git a/red/nodes/Node.js b/red/nodes/Node.js index be24de18c..3070ec932 100644 --- a/red/nodes/Node.js +++ b/red/nodes/Node.js @@ -186,7 +186,7 @@ function log_helper(self, level, msg) { } Node.prototype.log = function(msg) { - log_helper(this, 'log', msg); + log_helper(this, 'info', msg); }; Node.prototype.warn = function(msg) { diff --git a/red/red.js b/red/red.js index 715c4f776..96b11edb8 100644 --- a/red/red.js +++ b/red/red.js @@ -36,6 +36,7 @@ var RED = { userSettings.version = this.version(); settings.init(userSettings); server.init(httpServer,settings); + log.init(userSettings); return server.app; }, diff --git a/settings.js b/settings.js index 0c15b80b1..27f1f6209 100644 --- a/settings.js +++ b/settings.js @@ -132,6 +132,18 @@ module.exports = { // os:require('os'), // bonescript:require('bonescript'), // arduino:require('duino') - } + }, + + // Level of logging to be recorded. Options are: + // fatal - only those errors which make the application unusable should be recorded + // error - record errors which are deemed fatal for a particular request + fatal errors + // warn - record problems which are non fatal + errors + fatal errors + // info - record information about the general running of the application + warn + error + fatal errors + // debug - record information which is more verbose than info + info + warn + error + fatal errors + // trace - record very detailed logging + debug + info + warn + error + fatal errors + logLevel: "info", + + // Whether metrics are reported + metricsOn: false } diff --git a/test/nodes/core/core/58-debug_spec.js b/test/nodes/core/core/58-debug_spec.js index d4d877d63..9c1d0db13 100644 --- a/test/nodes/core/core/58-debug_spec.js +++ b/test/nodes/core/core/58-debug_spec.js @@ -70,11 +70,11 @@ describe('debug node', function() { try { helper.log().called.should.be.true; var logEvents = helper.log().args.filter(function(evt) { - return evt[0].level == "log"; + return evt[0].level == "info"; }); logEvents.should.have.length(1); var tstmp = logEvents[0][0].timestamp; - logEvents[0][0].should.eql({level:'log',id:'n1',type:'debug',msg:'test', timestamp:tstmp}); + logEvents[0][0].should.eql({level:'info',id:'n1',type:'debug',msg:'test', timestamp:tstmp}); done(); } catch(err) { diff --git a/test/nodes/core/parsers/70-HTML_spec.js b/test/nodes/core/parsers/70-HTML_spec.js index 8e31368e5..5209eb677 100644 --- a/test/nodes/core/parsers/70-HTML_spec.js +++ b/test/nodes/core/parsers/70-HTML_spec.js @@ -136,7 +136,7 @@ describe('html node', function() { n1.receive({payload:null,topic: "bar"}); helper.log().called.should.be.true; var logEvents = helper.log().args.filter(function(evt) { - return evt[0].level == "log"; + return evt[0].level == "info"; }); logEvents.should.have.length(1); // Each logEvent is the array of args passed to the function. diff --git a/test/nodes/core/parsers/70-JSON_spec.js b/test/nodes/core/parsers/70-JSON_spec.js index 3b5504560..ef9aa6390 100644 --- a/test/nodes/core/parsers/70-JSON_spec.js +++ b/test/nodes/core/parsers/70-JSON_spec.js @@ -82,7 +82,7 @@ describe('JSON node', function() { jn1.receive({payload:'foo',topic: "bar"}); helper.log().called.should.be.true; var logEvents = helper.log().args.filter(function(evt) { - return evt[0].level == "log"; + return evt[0].level == "info"; }); logEvents.should.have.length(1); logEvents[0][0].should.have.a.property('msg',"SyntaxError: Unexpected token o"+ "\nfoo"); @@ -104,7 +104,7 @@ describe('JSON node', function() { jn1.receive({payload:1,topic: "bar"}); helper.log().called.should.be.true; var logEvents = helper.log().args.filter(function(evt) { - return evt[0].level == "log"; + return evt[0].level == "info"; }); logEvents.should.have.length(1); logEvents[0][0].should.have.a.property('msg',"dropped: 1"); diff --git a/test/nodes/core/parsers/70-XML_spec.js b/test/nodes/core/parsers/70-XML_spec.js index d83360ac0..575e5412b 100644 --- a/test/nodes/core/parsers/70-XML_spec.js +++ b/test/nodes/core/parsers/70-XML_spec.js @@ -111,7 +111,7 @@ describe('XML node', function() { try { helper.log().called.should.be.true; var logEvents = helper.log().args.filter(function(evt) { - return evt[0].level == "log"; + return evt[0].level == "info"; }); logEvents.should.have.length(1); logEvents[0][0].should.have.a.property('msg',"This node only handles xml strings or js objects."); diff --git a/test/red/nodes/Node_spec.js b/test/red/nodes/Node_spec.js index a5f0ca9b5..4f7226e34 100644 --- a/test/red/nodes/Node_spec.js +++ b/test/red/nodes/Node_spec.js @@ -319,7 +319,7 @@ describe('Node', function() { loginfo = msg; }); n.log("a log message"); - should.deepEqual({level:"log", id:n.id, + should.deepEqual({level:"info", id:n.id, type:n.type, msg:"a log message", }, loginfo); Log.log.restore(); done(); @@ -334,7 +334,7 @@ describe('Node', function() { loginfo = msg; }); n.log("a log message"); - should.deepEqual({level:"log", id:n.id, name: "barney", + should.deepEqual({level:"info", id:n.id, name: "barney", type:n.type, msg:"a log message"}, loginfo); Log.log.restore(); done();