1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/log.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-05-03 23:26:35 +02:00
/**
2015-02-03 23:02:26 +01:00
* Copyright 2014, 2015 IBM Corp.
2014-05-03 23:26:35 +02:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var levels = {
2015-02-04 23:28:17 +01:00
off: 1,
2015-02-03 23:02:26 +01:00
fatal: 10,
error: 20,
warn: 30,
info: 40,
debug: 50,
trace: 60,
metric: 99
}
var levelNames = {
10: "fatal",
20: "error",
30: "warn",
40: "info",
50: "debug",
60: "trace",
2015-02-04 21:44:07 +01:00
99: "metric"
}
2014-05-03 23:26:35 +02:00
var logHandlers = [];
2015-02-04 21:44:07 +01:00
var metricsEnabled = false;
2015-02-03 23:02:26 +01:00
var ConsoleLogHandler = function(settings) {
this.logLevel = levels[settings.level]||levels.info;
this.metricsOn = settings.metrics||false;
2015-02-04 21:44:07 +01:00
metricsEnabled = this.metricsOn;
2015-02-03 23:02:26 +01:00
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);
}
}
2015-02-03 23:02:26 +01:00
});
}
2015-02-03 23:02:26 +01:00
util.inherits(ConsoleLogHandler, EventEmitter);
2015-02-03 23:02:26 +01:00
ConsoleLogHandler.prototype.shouldReportMessage = function(msglevel) {
return msglevel <= this.logLevel || (msglevel == log.METRIC && this.metricsOn);
}
2014-05-03 23:26:35 +02:00
var log = module.exports = {
2015-02-03 23:02:26 +01:00
FATAL: 10,
ERROR: 20,
WARN: 30,
INFO: 40,
DEBUG: 50,
TRACE: 60,
METRIC: 99,
init: function(settings) {
logHandlers = [];
2015-02-03 23:02:26 +01:00
var consoleSettings = {};
if (settings.logging) {
consoleSettings = settings.logging.console || {};
}
2015-02-03 23:02:26 +01:00
log.addHandler(new ConsoleLogHandler(consoleSettings));
},
2014-05-03 23:26:35 +02:00
addHandler: function(func) {
logHandlers.push(func);
2014-05-03 23:26:35 +02:00
},
log: function(msg) {
2015-01-27 15:41:20 +01:00
msg.timestamp = Date.now();
logHandlers.forEach(function(handler) {
handler.emit("log",msg);
2014-07-02 00:46:25 +02:00
});
2015-02-03 23:02:26 +01:00
},
info: function(msg) {
log.log({level:log.INFO,msg:msg});
},
warn: function(msg) {
log.log({level:log.WARN,msg:msg});
2015-02-04 21:44:07 +01:00
},
2015-02-05 18:05:39 +01:00
error: function(msg) {
log.log({level:log.ERROR,msg:msg});
},
trace: function(msg) {
log.log({level:log.TRACE,msg:msg});
},
debug: function(msg) {
log.log({level:log.DEBUG,msg:msg});
},
2015-02-04 21:44:07 +01:00
metric: function() {
return metricsEnabled;
2014-05-03 23:26:35 +02:00
}
}