diff --git a/red/log.js b/red/log.js index f41ce4fb3..e730c3e0f 100644 --- a/red/log.js +++ b/red/log.js @@ -42,26 +42,31 @@ 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; 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 +78,23 @@ var log = module.exports = { init: function(settings) { logHandlers = []; - var consoleSettings = {}; + var loggerSettings = {}; if (settings.logging) { - consoleSettings = settings.logging.console || {}; + var keys = Object.keys(settings.logging); + if (keys.length === 0) { + log.addHandler(new LogHandler()); + } else { + for (var i=0, l=keys.length; i