-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.js
107 lines (94 loc) · 3.64 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var path = require('path');
var chalk = require('chalk'); // env FORCE_COLOR=1 must be set
var myLib = require('./myLib');
var appConfig = require("./etc/app.json");
var production = process.env.NODE_ENV === 'production';
var logstamp = function(timestamp) {
let tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
let localISOTime = (new Date(timestamp - tzoffset)).toISOString();
return localISOTime.slice(0, production ? -5 : -1).replace('T', ' ');
};
console.error(chalk.bold(logstamp(Date.now())), '*', production ? chalk.green("PRODUCTION") : chalk.magenta("DEVELOPMENT"), "ENV * node", chalk.cyan(process.version), "initialized with pid", process.pid, "in", chalk.yellow(__dirname));
module.exports = function (filename, debugEnabled) {
if (typeof debugEnabled !== 'boolean') {
debugEnabled = !production;
}
var scriptName = path.basename(filename) + ":";
/* Disabled not to interfere with json output
var info = function (title, ...data) {
console.log("···", logstamp(Date.now()), debugEnabled ? scriptName : 'INFO', title, ...data);
};
this.info = info; */
var notice = function (title, ...data) {
console.error(chalk.green.inverse("···"), logstamp(Date.now()), chalk.green.bold(debugEnabled ? 'NOTICE' : '\u2714'), chalk.yellow(scriptName), chalk.bold(title), ...data);
};
this.notice = notice;
var debug = function (title, ...data) {
console.error(chalk.bold("***"), logstamp(Date.now()), chalk.bold('DEBUG'), chalk.white(scriptName), chalk.cyan(title), ...data);
return true;
};
this.debug = debugEnabled ? debug : () => false;
var email = function (title, ...data) {
if (debugEnabled) {
debug("send-email", [title, ...data]);
}
let body = [...data].map((item) => {
if (item) {
return typeof item === 'object' ? JSON.stringify(item, null, 4) : item.toString();
} else {
return '';
}
});
myLib.mailSend([__dirname, scriptName].join('/') + [' ', title, logstamp(Date.now())].join(' '), body.join("\n"), appConfig.adminEmail);
};
this.email = email;
var error = function (title, ...data) {
if (!debugEnabled) {
email(title, ...data);
}
console.error(chalk.red.inverse('==='), logstamp(Date.now()), chalk.red.bold('ERROR'), chalk.yellow(scriptName), chalk.bold(title), ...data);
};
this.error = error;
var warning = function (title, ...data) {
console.error(chalk.magenta.inverse('---'), logstamp(Date.now()), chalk.magenta.bold('WARNING'), chalk.yellow(scriptName), chalk.bold(title), ...data);
};
this.warning = warning;
this.getNanoseconds = function () {
let hrtime = process.hrtime();
return hrtime[0] * 1000000000 + hrtime[1];
};
function jsonLog(metadata, labels, tags, values, extra) {
if (typeof labels === 'string') {
labels = [labels];
}
let tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
let timestamp = Date.now();
let localISOTime = (new Date(timestamp - tzoffset)).toISOString();
let debugEntry = JSON.stringify({
logStamp: localISOTime.slice(0, -1),
timestamp: timestamp,
metadata: metadata,
labels: labels,
tags: tags,
values: values,
extra: extra
});
//websocket.emitDebugEvent(debugEntry);
console.log(debugEntry);
/*
if (values !== null && typeof (values) === 'object') {
values = JSON.stringify(values, null, 4);
}
if (extra !== null && typeof (extra) === 'object') {
extra = JSON.stringify(extra, null, 4);
}
let errorLabels = ["warning", "error", "panic", "alert"];
labels.forEach(function (label){
if (errorLabels.indexOf(label) !== -1) {
console.error(">>>>", debugEntry.logStamp, labels, tags, values, metadata, extra);
}
});
*/
}
this.json = jsonLog;
};