2014-05-03 23:26:35 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
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;
|
|
|
|
|
2015-05-08 15:21:01 +02:00
|
|
|
var i18n = require("./i18n");
|
|
|
|
|
2015-01-29 14:48:50 +01:00
|
|
|
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,
|
2015-05-14 15:22:28 +02:00
|
|
|
audit: 98,
|
2015-02-03 23:02:26 +01:00
|
|
|
metric: 99
|
2015-03-06 15:14:47 +01:00
|
|
|
};
|
|
|
|
|
2015-02-03 23:02:26 +01:00
|
|
|
var levelNames = {
|
|
|
|
10: "fatal",
|
|
|
|
20: "error",
|
|
|
|
30: "warn",
|
|
|
|
40: "info",
|
|
|
|
50: "debug",
|
|
|
|
60: "trace",
|
2015-05-14 15:22:28 +02:00
|
|
|
98: "audit",
|
2015-02-04 21:44:07 +01:00
|
|
|
99: "metric"
|
2015-03-06 15:14:47 +01:00
|
|
|
};
|
2015-01-29 14:48:50 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
var logHandlers = [];
|
|
|
|
|
2017-03-01 18:00:27 +01:00
|
|
|
var verbose;
|
|
|
|
|
2015-02-04 21:44:07 +01:00
|
|
|
var metricsEnabled = false;
|
|
|
|
|
2015-04-30 23:01:22 +02:00
|
|
|
var LogHandler = function(settings) {
|
|
|
|
this.logLevel = settings ? levels[settings.level]||levels.info : levels.info;
|
|
|
|
this.metricsOn = settings ? settings.metrics||false : false;
|
2015-05-14 15:22:28 +02:00
|
|
|
this.auditOn = settings ? settings.audit||false : false;
|
2015-11-24 23:38:42 +01:00
|
|
|
|
2015-05-14 15:22:28 +02:00
|
|
|
metricsEnabled = metricsEnabled || this.metricsOn;
|
2015-11-24 23:38:42 +01:00
|
|
|
|
2015-04-30 23:01:22 +02:00
|
|
|
this.handler = (settings && settings.handler) ? settings.handler(settings) : consoleLogger;
|
2015-02-03 23:02:26 +01:00
|
|
|
this.on("log",function(msg) {
|
|
|
|
if (this.shouldReportMessage(msg.level)) {
|
2015-04-30 23:01:22 +02:00
|
|
|
this.handler(msg);
|
2015-01-29 14:48:50 +01:00
|
|
|
}
|
2015-02-03 23:02:26 +01:00
|
|
|
});
|
2015-01-29 14:48:50 +01:00
|
|
|
}
|
2015-04-30 23:01:22 +02:00
|
|
|
util.inherits(LogHandler, EventEmitter);
|
2015-01-29 14:48:50 +01:00
|
|
|
|
2015-04-30 23:01:22 +02:00
|
|
|
LogHandler.prototype.shouldReportMessage = function(msglevel) {
|
2015-06-02 17:23:38 +02:00
|
|
|
return (msglevel == log.METRIC && this.metricsOn) ||
|
|
|
|
(msglevel == log.AUDIT && this.auditOn) ||
|
|
|
|
msglevel <= this.logLevel;
|
2015-02-03 23:02:26 +01:00
|
|
|
}
|
2015-01-29 14:48:50 +01:00
|
|
|
|
2015-04-30 23:01:22 +02:00
|
|
|
var consoleLogger = function(msg) {
|
2015-05-14 15:22:28 +02:00
|
|
|
if (msg.level == log.METRIC || msg.level == log.AUDIT) {
|
|
|
|
util.log("["+levelNames[msg.level]+"] "+JSON.stringify(msg));
|
2015-04-30 23:01:22 +02:00
|
|
|
} else {
|
2018-01-14 01:50:58 +01:00
|
|
|
if (verbose && msg.msg && msg.msg.stack) {
|
2017-03-01 18:00:27 +01:00
|
|
|
util.log("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+msg.msg.stack);
|
|
|
|
} else {
|
|
|
|
var message = msg.msg;
|
2018-01-14 01:50:58 +01:00
|
|
|
if (typeof message === 'object' && message !== null && message.toString() === '[object Object]' && message.message) {
|
2017-03-01 18:00:27 +01:00
|
|
|
message = message.message;
|
|
|
|
}
|
|
|
|
util.log("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+message);
|
2016-06-12 12:07:05 +02:00
|
|
|
}
|
2015-04-30 23:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2015-05-14 15:22:28 +02:00
|
|
|
AUDIT: 98,
|
2015-02-03 23:02:26 +01:00
|
|
|
METRIC: 99,
|
2015-03-06 15:14:47 +01:00
|
|
|
|
2015-01-29 14:48:50 +01:00
|
|
|
init: function(settings) {
|
2015-05-14 15:22:28 +02:00
|
|
|
metricsEnabled = false;
|
2015-02-04 22:29:11 +01:00
|
|
|
logHandlers = [];
|
2015-04-30 23:01:22 +02:00
|
|
|
var loggerSettings = {};
|
2017-03-01 18:00:27 +01:00
|
|
|
verbose = settings.verbose;
|
2015-05-01 15:37:23 +02:00
|
|
|
if (settings.logging) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 23:01:22 +02:00
|
|
|
}
|
2015-05-01 15:37:23 +02:00
|
|
|
} else {
|
|
|
|
log.addHandler(new LogHandler());
|
2015-01-29 14:48:50 +01:00
|
|
|
}
|
|
|
|
},
|
2014-05-03 23:26:35 +02:00
|
|
|
addHandler: function(func) {
|
2014-05-06 14:05:11 +02:00
|
|
|
logHandlers.push(func);
|
2014-05-03 23:26:35 +02:00
|
|
|
},
|
2016-10-21 00:31:40 +02:00
|
|
|
removeHandler: function(func) {
|
|
|
|
var index = logHandlers.indexOf(func);
|
|
|
|
if (index > -1) {
|
|
|
|
logHandlers.splice(index,1);
|
|
|
|
}
|
|
|
|
},
|
2014-05-03 23:26:35 +02:00
|
|
|
log: function(msg) {
|
2015-01-27 15:41:20 +01:00
|
|
|
msg.timestamp = Date.now();
|
2014-07-02 17:31:02 +02:00
|
|
|
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;
|
2015-05-14 15:22:28 +02:00
|
|
|
},
|
2015-11-24 23:38:42 +01:00
|
|
|
|
2015-05-14 15:22:28 +02:00
|
|
|
audit: function(msg,req) {
|
|
|
|
msg.level = log.AUDIT;
|
|
|
|
if (req) {
|
|
|
|
msg.user = req.user;
|
|
|
|
msg.path = req.path;
|
|
|
|
msg.ip = (req.headers && req.headers['x-forwarded-for']) || (req.connection && req.connection.remoteAddress) || undefined;
|
|
|
|
}
|
|
|
|
log.log(msg);
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 15:21:01 +02:00
|
|
|
|
|
|
|
log["_"] = i18n._;
|