forked from Juravenator/formal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
312 lines (274 loc) · 8.64 KB
/
index.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
'use strict';
const term = require('terminal-kit').terminal;
const moment = require('moment');
const stringz = require('stringz'); // for emoji support ❤️
const request = require('request');
const stringify = require('circular-json').stringify;
var PrettyError;
var pe = {};
const gelfLevel = {
log: 1,
error: 3,
warn: 4,
info: 6,
debug: 7,
success: 8,
};
/*
.init({enable: ['log', 'debug', 'info', 'warn', 'error'], PrettyError: true})
*/
module.exports.init = opt => {
(opt.enable || ['debug', 'log', 'info', 'warn', 'error']).forEach(type => {
module.exports.makeSimpleLogger(type);
});
if (opt.PrettyError) {
PrettyError = require('pretty-error');
pe = new PrettyError();
pe.skipNodeFiles();
pe.skipPackage('loader.js', 'chai', 'when');
pe.skipPath(
'internal/main/run_main_module.js',
'internal/modules/cjs/loader.js',
'internal/modules/cjs/helpers.js'
);
}
};
// graylog init
module.exports.grayLog = opt => {
module.exports.options.grayLog = {
logHost: opt.logHost || null,
host: opt.host || null,
port: opt.port || 12201,
path: opt.path || '/gelf',
scope: opt.configureScope || null,
reservedKeys: opt.reservedKeys || [],
};
(opt.enable || ['debug', 'log', 'warn', 'error']).forEach(type => {
module.exports.enableGraylog(type);
});
};
// keep the native pipe to stdout & stderr
module.exports.nativeLog = global.console.log;
module.exports.nativeError = global.console.error;
// enables or disables certain types of logging
var loggerTypes = {};
module.exports.enable = type => {
module.exports.options.styles[type] = module.exports.options.styles[type] || [
term,
module.exports.nativeLog,
];
loggerTypes[type] = true;
};
module.exports.disable = type => (loggerTypes[type] = false);
module.exports.isEnabled = type => loggerTypes[type];
// enables or disables certain types of logging
var loggerTypesGraylog = {};
module.exports.enableGraylog = type => {
loggerTypesGraylog[type] = true;
};
module.exports.disableGraylog = type => (loggerTypesGraylog[type] = false);
module.exports.isEnabledGraylog = type => loggerTypesGraylog[type];
module.exports.options = {
typePadding: ' ', // dictates the width of the type prefix
styles: {
// contains term styles for the various prefixes
error: [term.error.bgRed.white, module.exports.nativeError],
warn: [term.error.bgYellow.white, module.exports.nativeError],
info: [term, module.exports.nativeLog],
debug: [term.green, module.exports.nativeLog],
success: [term.bgGreen.white, module.exports.nativeLog],
},
// a function that takes a date and returns a string
// used to print the date in the prefix
dateFormatter: date => moment(date).format('HH:mm:ss.SSS'),
quitOnException: true,
grayLog: {
enabled: false,
},
};
const getLogTypePrefix = type =>
` [${type}] ${module.exports.options.typePadding.substring(
stringz.length(type) + 4
)}`;
const getPrefix = type =>
getLogTypePrefix(type) +
module.exports.options.dateFormatter(new Date()) +
' ';
module.exports.printPrefix = (type, t = term) => {
t(getPrefix(type));
t.styleReset('| ');
};
function prepareLog(message) {
let result = [];
if (Object.prototype.toString.call(message) === '[object Error]') {
if (pe) {
result.push(pe.render(message));
} else {
result.push(
`Error: ${message.code || ''} ${message.message || message}`
);
if (message.hasOwnProperty('stack')) {
result.push(`\nstack: ${message.stack}`);
}
result.push(
`\nprocess versions: ${stringify(process.versions, null, 5)}`
);
result.push(
`\nmemory usage: ${stringify(process.memoryUsage(), null, 5)}`
);
}
} else if (Object.prototype.toString.call(message) === '[object Array]') {
result.push(stringify(message, null, 5));
} else if (Object.prototype.toString.call(message) === '[object Object]') {
result.push(stringify(message, null, 5));
} else {
result.push(message);
}
return result; //result.length <= 1 ? result.join() :
}
function simpleLogger(logData) {
let TYPE =
logData.messageType == 'success'
? 'O'
: logData.messageType.substr(0, 1).toUpperCase();
//module.exports.nativeLog('DEBUG! simpleLogger()', {logData});
let locMsg = prepareLog(logData.messages[0]);
for (let i = 1; i < logData.messages.length; i++) {
let message = logData.messages[i];
if (Object.prototype.toString.call(message) === '[object Object]') {
if (module.exports.options.grayLog.reservedKeys) {
const keys = Object.keys(message);
for (let k = 0; k < keys.length; k++) {
if (
module.exports.options.grayLog.reservedKeys.find(
reservedKey => reservedKey == keys[k]
)
) {
delete message[keys[k]];
}
}
}
}
if (Object.keys(message).length) {
locMsg.push(...['\n'], ...prepareLog(message));
}
}
if (loggerTypes[logData.messageType]) {
module.exports.printPrefix(
TYPE,
module.exports.options.styles[logData.messageType][0]
);
module.exports.options.styles[logData.messageType][1].apply(this, locMsg);
}
sendHTTPGelf(logData);
}
module.exports.makeSimpleLogger = type => {
module.exports.enable(type);
global.console[type] = function() {
simpleLogger({
messageType: type,
messages: arguments,
_messagesObj_type: Object.prototype.toString.call(arguments),
});
};
};
module.exports.makeCustomLogger = (type, myfunction) => {
module.exports.enable(type);
global.console[type] = function() {
if (loggerTypes[type]) {
myfunction.apply(this, arguments);
}
if (loggerTypesGraylog[type]) {
sendHTTPGelf(arguments);
}
};
};
function sendHTTPGelf(logData) {
if (
!module.exports.options.grayLog.host ||
!loggerTypesGraylog[logData.messageType]
) {
return;
}
let locMsg = {
version: '1.1',
host: module.exports.options.grayLog.logHost,
level: gelfLevel[logData.messageType] || gelfLevel['info'],
_local_timestamp: moment().toISOString(true),
full_message: '',
};
if (
Object.prototype.toString.call(logData.messages[0]) === '[object Error]'
) {
if (pe) {
pe.withoutColors();
let peRender = pe.render(logData.messages[0]).split('\n');
locMsg.short_message = peRender[0];
peRender.shift();
locMsg.full_message = peRender.join('\n');
} else {
locMsg.short_message = `Error: ${logData.messages[0].code || ''} ${logData
.messages[0].message || logData.messages[0]}`;
locMsg.full_message = '';
if (logData.messages[0].hasOwnProperty('stack')) {
locMsg.full_message = logData.messages[0].stack;
}
locMsg.full_message += `\nprocess versions: ${stringify(
process.versions,
null,
5
)}\nmemory usage: ${stringify(process.memoryUsage(), null, 5)}`;
}
} else {
locMsg.short_message = prepareLog(logData.messages[0]).join();
}
for (let i = 1; i < logData.messages.length; i++) {
if (
Object.prototype.toString.call(logData.messages[i]) === '[object Object]'
) {
Object.keys(logData.messages[i]).forEach(key => {
locMsg[`_${key}`] = prepareLog(logData.messages[i][key]).join('\n');
});
} else {
locMsg.full_message += `${!locMsg.full_message ? '' : '\n'}${prepareLog(
logData.messages[i]
).join('\n')}`;
}
}
if (module.exports.options.grayLog.scope) {
Object.assign(locMsg, module.exports.options.grayLog.scope());
}
const options = {
uri: `http://${module.exports.options.grayLog.host}:${module.exports.options.grayLog.port}${module.exports.options.grayLog.path}`,
method: 'POST',
timeout: 1500,
json: true,
body: locMsg,
};
//module.exports.nativeLog('>>> locMsg:', locMsg)
request.debug = false;
// eslint-disable-next-line no-unused-vars
request(options, function(error, response, body) {
if (error) {
//return console.error(error);
//module.exports.nativeLog('sendHTTPGelf: error: ', error);
}
//console.log('Upload successful! Server responded with:', body);
//module.exports.nativeLog('sendHTTPGelf() response: ', {
//options,
//statusCode: response.statusCode,
//body,
//});
});
}
const exception = {
// todo: add info about host, environment to messages
handler(err) {
console.error(err);
if (module.exports.options.grayLog.quitOnException) process.exit(1);
},
};
module.exports.handleExceptions = () => {
process.on('unhandledRejection', exception.handler.bind(exception));
process.on('uncaughtException', exception.handler.bind(exception));
};