mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Added the capability to register custom loggers (for Issue #626)
This commit is contained in:
parent
c016b102eb
commit
276d893198
46
red/log.js
46
red/log.js
@ -42,26 +42,36 @@ var logHandlers = [];
|
||||
|
||||
var metricsEnabled = false;
|
||||
|
||||
var ConsoleLogHandler = function(settings) {
|
||||
this.logLevel = levels[settings.level]||levels.info;
|
||||
this.metricsOn = settings.metrics||false;
|
||||
var LogHandler = function(settings) {
|
||||
this.logLevel = settings ? levels[settings.level]||levels.info : levels.info;
|
||||
this.metricsOn = settings ? settings.metrics||false : false;
|
||||
metricsEnabled = this.metricsOn;
|
||||
this.handler = (settings && settings.handler) ? settings.handler(settings) : consoleLogger;
|
||||
//if (settings && settings.handler) {
|
||||
//this.handler = settings.handler(settings);
|
||||
//} else {
|
||||
//this.handler = consoleLogger;
|
||||
//}
|
||||
this.on("log",function(msg) {
|
||||
if (this.shouldReportMessage(msg.level)) {
|
||||
if (msg.level == log.METRIC) {
|
||||
util.log("[metric] "+JSON.stringify(msg));
|
||||
} else {
|
||||
util.log("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+msg.msg);
|
||||
}
|
||||
this.handler(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
util.inherits(ConsoleLogHandler, EventEmitter);
|
||||
util.inherits(LogHandler, EventEmitter);
|
||||
|
||||
ConsoleLogHandler.prototype.shouldReportMessage = function(msglevel) {
|
||||
LogHandler.prototype.shouldReportMessage = function(msglevel) {
|
||||
return msglevel <= this.logLevel || (msglevel == log.METRIC && this.metricsOn);
|
||||
}
|
||||
|
||||
var consoleLogger = function(msg) {
|
||||
if (msg.level == log.METRIC) {
|
||||
util.log("[metric] "+JSON.stringify(msg));
|
||||
} else {
|
||||
util.log("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+msg.msg);
|
||||
}
|
||||
}
|
||||
|
||||
var log = module.exports = {
|
||||
FATAL: 10,
|
||||
ERROR: 20,
|
||||
@ -73,11 +83,19 @@ var log = module.exports = {
|
||||
|
||||
init: function(settings) {
|
||||
logHandlers = [];
|
||||
var consoleSettings = {};
|
||||
if (settings.logging) {
|
||||
consoleSettings = settings.logging.console || {};
|
||||
var loggerSettings = {};
|
||||
var keys = Object.keys(settings.logging);
|
||||
if (keys.length === 0) {
|
||||
log.addHandler(new LogHandler());
|
||||
} else {
|
||||
for (var i=0, l=keys.length; i<l; i++) {
|
||||
var config = settings.logging[keys[i]];
|
||||
loggerSettings = config || {};
|
||||
//if ((keys[i] === "console") || config.handler) {
|
||||
log.addHandler(new LogHandler(loggerSettings));
|
||||
//}
|
||||
}
|
||||
}
|
||||
log.addHandler(new ConsoleLogHandler(consoleSettings));
|
||||
},
|
||||
addHandler: function(func) {
|
||||
logHandlers.push(func);
|
||||
|
@ -144,4 +144,20 @@ describe("red/log", function() {
|
||||
sinon.assert.neverCalledWithMatch(util.log,"[trace] This is a trace");
|
||||
sinon.assert.neverCalledWithMatch(util.log,"[metric] ");
|
||||
});
|
||||
it('uses console logger if custom logger handler does not exist', function() {
|
||||
var settings = {logging: { customLogger: { level: 'trace', metrics: true } } };
|
||||
log.init(settings);
|
||||
log.error("This is an error");
|
||||
log.warn("This is a warn");
|
||||
log.info("This is an info");
|
||||
log.debug("This is a debug");
|
||||
log.trace("This is a trace");
|
||||
log.log({level:log.METRIC,msg:"testMetric"});
|
||||
sinon.assert.calledWithMatch(util.log,"[error] This is an error");
|
||||
sinon.assert.calledWithMatch(util.log,"[warn] This is a warn");
|
||||
sinon.assert.calledWithMatch(util.log,"[info] This is an info");
|
||||
sinon.assert.calledWithMatch(util.log,"[debug] This is a debug");
|
||||
sinon.assert.calledWithMatch(util.log,"[trace] This is a trace");
|
||||
sinon.assert.calledWithMatch(util.log,"[metric] ");
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user