-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (62 loc) · 1.88 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
'use strict';
var getenv = require('getenv');
var winston = require('winston');
// Just take over stream, don't add format
// streaming to morgan or expressWinston error logs
var unformattedLog = new winston.Logger({
transports: [
new winston.transports.Console({
level: 'info',
formatter: function(options) {
return options.message
},
})
],
exitOnError: false
});
var log = new winston.Logger({
transports: [
new winston.transports.Console({
timestamp: function() {
return new Date().toISOString();
},
formatter: function(options) {
// Return string will be passed to logger.
var stack_trace = '';
if(options.meta && Object.keys(options.meta).length && options.meta.stack) {
if(typeof options.meta.stack === 'string') {
stack_trace = options.meta.stack
} else {
stack_trace = options.meta.stack.join('\n');
}
}
return '[' + options.level.toUpperCase() + '] ' +
options.timestamp() + ' ' +
(options.message !== undefined ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? ' | ' +
JSON.stringify(options.meta) : '' ) + stack_trace;
},
level: getenv('LOG_LEVEL', 'debug'),
handleExceptions: true,
humanReadableUnhandledException: true,
json: false,
colorize: true
})
],
exitOnError: false
});
var errorLogger = function(err, req, res, next) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
log.error(req.method + ' ' + req.url + ' ' + ip + ' - ' +
err.message,
winston.exception.getAllInfo(err)
);
next(err);
}
module.exports = log;
module.exports.errorLogger = errorLogger;
module.exports.stream = {
write: function(message, encoding){
unformattedLog.info(message);
}
};