-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
68 lines (66 loc) · 2.81 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var hasNode = false;
var hasBrowser = false;
try {
module.exports = Logger;
hasNode = true;
} catch (e) {
console.log("no node support, ", e);
window.Logger = Logger;
hasBrowser = true;
}
function Logger(minLogLevel, logCB, moduleName) {
var levels = ["DEBUG", "INFO", "WARN", "ERROR", "FAIL"];
var levelsAlt = ["VERBOSE", "INFORMATION", "WARNING", "ERR", "FATAL"];
/**
* Creates a log string to be written to a file or to the console
* @param {string} message
* @param {string} moduleName
* @param {string} level
* @param {Error?} error
*/
function getLog(message, moduleName, level, error) {
var timestamp = new Date().toISOString();
if (!error && level && (level.message || level.stack)) {
error = level;
level = "ERROR";
}
var logLevel = (typeof level === "number" ? levels[level] : (level ? level.toUpperCase() : null));
if (levelsAlt.indexOf(logLevel) !== -1) logLevel = levels[levelsAlt.indexOf(logLevel)];
else if (levels.indexOf(logLevel) === -1) logLevel = levels[1];
var logValue = levels.indexOf(logLevel);
if (error) {
logValue = Math.max(logValue, levels.indexOf("ERROR"));
logLevel = levels[logValue];
}
var errorMessage = error ? (error.stack ? (error.stack.replace(error.name, function (name) { return '(' + name + ')'; })) :
('(' + (error.name || "Error") + ')' + (error.message ? (": " + error.message.replace(/[\r\n]+^/, "")) : ""))) : null;
return {
timestamp: timestamp,
level: [logValue, logLevel],
message: message.replace(/[\r\n]+^/, ""),
module: moduleName || "UNKNOWN",
error: error ? errorMessage : null
};
}
function logString(log) {
return log.timestamp + " " + log.level[1] + "\t[" + log.module + "]:\t" +
log.message.replace(/\n[ \t]*/g, "\n ") + (log.error ? (": " + log.error) : "");
}
if (!minLogLevel) minLogLevel = levels.indexOf("INFO");
if (typeof minLogLevel === "string") minLogLevel = Math.max(
levels.indexOf(minLogLevel.toUpperCase()),
levelsAlt.indexOf(minLogLevel.toUpperCase())
);
if (moduleName) return function logMessage(message, level, error) {
var log = getLog(message, moduleName, level, error);
if (log.level[0] < minLogLevel) return;
logCB(logString(log), log.level);
};
else return function logMessage(message, moduleName, level, error) {
var log = getLog(message, moduleName, level, error);
if (log.level[0] < minLogLevel) return;
logCB(logString(log), log.level);
};
}
Logger.levels = ["DEBUG", "INFO", "WARN", "ERROR", "FAIL"];
Logger.levelsAlt = ["VERBOSE", "INFORMATION", "WARNING", "ERR", "FATAL"];