mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Adding logging & metric recording configuration via settings
This commit is contained in:
parent
a22f819f40
commit
b052324d36
55
red/log.js
55
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);
|
||||
},
|
||||
|
@ -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) {
|
||||
|
@ -36,6 +36,7 @@ var RED = {
|
||||
userSettings.version = this.version();
|
||||
settings.init(userSettings);
|
||||
server.init(httpServer,settings);
|
||||
log.init(userSettings);
|
||||
return server.app;
|
||||
},
|
||||
|
||||
|
14
settings.js
14
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
|
||||
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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.
|
||||
|
@ -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");
|
||||
|
@ -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.");
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user