-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.js
243 lines (225 loc) · 6.93 KB
/
log.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const fs = require('fs'), assert = require("assert"), util = require('util');
const LOG_LEVELS = {
debug: 0,
info: 1,
warn: 2,
error: 3,
fatal: 4
};
let curr_log_level = 2;
let fd;
let fd_uncork_timer;
let fd_buffer_flush_time;
let filename;
let airbrake;
let is_json_format = false;
let is_fatal = false;
for (let level of Object.keys(LOG_LEVELS)) {
let up_case_level = level.toUpperCase();
console[level] = function(...args) {
log(LOG_LEVELS[level], up_case_level, args);
};
}
function log(log_level, level_name, args) {
if (log_level >= curr_log_level) {
if (is_json_format) {
let obj = typeof args[0] === 'string' ? {msg: args[0]} : args[0];
obj['pid'] = process.pid;
obj['level'] = level_name;
obj['time'] = getTime();
console.log_ ? console.log_(JSON.stringify(obj)) : console.log(JSON.stringify(obj))
} else {
let s = '[' + getTime() + '] [' + level_name + '] ['+process.pid+'] ';
console.log(s, args);
}
}
}
function getTime() {
let d = new Date();
return d.getFullYear() + '-' + addZero(d.getMonth() + 1) + '-' + addZero(d.getDate()) + ' ' +
addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds()) +
'.' + add2Zero(d.getMilliseconds());
}
function addZero(val) {
return (val >= 10 ? '' : '0')+val;
}
function add2Zero(val) {
return (val >= 10 ? (val >= 100 ? '' : '0') : '00')+val;
}
// All system error -> console.fatal
process.on('uncaughtException', function(err) {
is_fatal = true;
if (is_json_format) {
console.fatal({msg: 'Uncaught exception', error: err.message || err, stack: err.stack});
} else {
console.fatal('Uncaught exception:', err.message || err, err.stack);
}
if (console._airbrake) {
sendAirbrake(err);
} else {
console.processExit(1);
}
});
process.on('unhandledRejection', function(err) {
is_fatal = true;
if (is_json_format) {
console.fatal({msg: 'Uncaught rejection', error: err.message || err, stack: err.stack});
} else {
console.fatal('Uncaught rejection:', err.message || err, err.stack);
}
if (console._airbrake) {
sendAirbrake(err);
} else {
console.processExit(1);
}
});
function sendAirbrake(err) {
let p = console._airbrake.notify(err, function(notify_err, url, devMode) {
if (notify_err) {
if (is_json_format) {
console.fatal({msg: 'Airbrake: Could not notify service.', error: notify_err.message || notify_err, stack: notify_err.stack});
} else {
console.fatal('Airbrake: Could not notify service:', notify_err.message || notify_err, notify_err.stack);
}
} else if (devMode) {
console.log('Airbrake: Dev mode, did not send.');
} else {
console.log('Airbrake: Notified service: ' + url);
}
console.processExit(1);
});
if (Promise.prototype.isPrototypeOf(p)) p.then(() => {
console.processExit(1);
}, (notify_err) => {
if (is_json_format) {
console.fatal({msg: 'Airbrake: Could not notify service.', error: notify_err.message || notify_err, stack: notify_err.stack});
} else {
console.fatal('Airbrake: Could not notify service:', notify_err.message || notify_err, notify_err.stack);
}
});
}
console.processExit = function(code) {
if (fd) {
fd.end(null, null, function() {
process.exit(code);
});
} else {
process.exit(code);
}
};
console.setLevel = function(level) {
level = (level || '').toLowerCase();
assert(LOG_LEVELS[level] != null, 'Log level shoud be:'+Object.keys(LOG_LEVELS).join(', '));
curr_log_level = LOG_LEVELS[level];
return console;
};
console.setAirbrake = function(_airbrake) {
if (_airbrake) console._airbrake = _airbrake;
return console;
};
/**
* Make sure that next packages are installed before using airbrake notifications:
* `request`, `airbrake-js`
* @param {Object} conf - Airbrake configuration, will be passed without changingto `airbrake-js`
* @param {String[]} [del_env_keys=[]] - Don't send process.env keys matching each del_env_keys pattern
* @param {Boolean} [notify_dev_env=false] - Send notifications to Airbrake using `dev` envs
* @return {Console}
*/
console.setAirbrakeByOptions = function(conf, del_env_keys = [], notify_dev_env = false) {
if (conf.projectId && conf.projectKey) {
require('request');
const AirbrakeClient = require('airbrake-js');
if (typeof conf.unwrapConsole == 'undefined') conf.unwrapConsole = true;
console._airbrake = new AirbrakeClient(conf);
del_env_keys = del_env_keys.map(pattern => new RegExp(pattern, 'i'));
console._airbrake.addFilter(notice => {
if (notice.context.environment.toLowerCase().startsWith('dev') && !notify_dev_env) {
return null;
}
for (let [key, val] of Object.entries(process.env)) {
if (!del_env_keys.some(r => r.test(key)) && !notice.environment[key])
notice.environment[key] = val;
}
if (is_fatal) notice.context.severity = 'critical';
return notice;
});
} else {
console.error('Airbrake is not set. Check config: projectId, projectKey');
}
return console;
};
console.setJSONFormat = function(json_format) {
is_json_format = json_format;
if (json_format && !('toJSON' in Error.prototype)) {
Object.defineProperty(Error.prototype, 'toJSON', {
value() {
let alt = {};
Object.getOwnPropertyNames(this).forEach(key => {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true,
writable: true
});
}
return console;
};
console.setFileBufferFlushTime = function(time) {
fd_buffer_flush_time = time;
if (fd_uncork_timer) {
clearInterval(fd_uncork_timer);
fd_uncork_timer = null;
}
if (fd) {
if (!time) {
fd.uncork();
} else {
fd.cork();
fd_uncork_timer = setInterval(function() {
fd.uncork();
fd.cork();
}, fd_buffer_flush_time);
}
}
return console;
};
console.setFile = function(file, callback) {
if (!file) {
if (callback) callback();
} else {
fd = fs.createWriteStream(file, {flags: 'a', mode: 0o644, encoding: 'utf8'});
fd.addListener('error', function(err) {
if (is_json_format) {
console.fatal({msg: 'Log create error', error: err});
} else {
console.fatal('Log create error', err);
}
if (callback) callback(err);
}).addListener('open', function() {
console.log = function(...args) {
if (fd.writable) fd.write(util.format(...args) + '\n');
};
console.log_ = function(str) {
if (fd.writable) fd.write(str + '\n');
};
filename = file;
console.setFileBufferFlushTime(fd_buffer_flush_time);
if (callback) callback();
});
}
return console;
};
console.reopen = function(callback) {
if (fd) {
fd.end();
console.setFile(filename, function() {
console.log('Reopen log file by SIGUSR1');
if (callback) callback();
});
}
};
process.on('SIGUSR1', function() {
console.reopen();
});
module.exports = console;