-
Notifications
You must be signed in to change notification settings - Fork 12
/
logger.js
123 lines (104 loc) · 3.37 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const winston = require('winston');
const os = require('os');
const { createLogger, format, transports } = require('winston');
const { inspect } = require('util');
const isMobile = os.platform() === 'android' || os.platform() === 'ios';
// From https://github.com/winstonjs/winston/issues/1427#issuecomment-535297716
function isPrimitive(val) {
return val === null || (typeof val !== 'object' && typeof val !== 'function');
}
function formatWithInspect(val, colorize) {
const prefix = isPrimitive(val) ? '' : '\n';
const shouldFormat = typeof val !== 'string';
return prefix + (shouldFormat ? inspect(val, { depth: null, colors: colorize }) : val);
}
function fancyPrintf(colorize) {
return function(info) {
const msg = formatWithInspect(info.message, colorize);
const splatArgs = info[Symbol.for('splat')] || [];
const rest = splatArgs.map(data => formatWithInspect(data)).join(' ');
return `${info.timestamp} - ${info.level}: ${msg} ${rest}`;
};
}
const monochromeFormat = format.combine(
format.timestamp(),
format.errors({ stack: true }),
format.printf(fancyPrintf(!isMobile))
);
const colorizedFormat = format.combine(
format.timestamp(),
format.errors({ stack: true }),
format.colorize(),
format.printf(fancyPrintf(!isMobile))
);
const logger = createLogger({
level: process.env.LOG_LEVEL || 'debug',
format: winston.format.combine(
isMobile ? monochromeFormat : colorizedFormat,
winston.format(info => {
if (info.message &&
typeof info.message === 'string' &&
info.message.includes('Possibly unsupported ZIP platform type')) {
return false; // Ignore logs from decompress-zip structures.js when decompressing target.dat
}
return info;
})()
),
transports: [new transports.Console()]
});
if (process.env.NODE_ENV === 'production') {
logger.level = 'info';
if (!isMobile) {
logger.add(new winston.transports.File({
filename: 'error.log',
level: 'error'
}));
}
}
/**
* Allows filtering log messages out if they don't come from a file listed in
* the LOG_MODULES environment variable.
* @return {boolean} True if we want to keep the log message
*/
function checkLogModules() {
if (!process.env.LOG_MODULES) {
return true;
}
const logModules = process.env.LOG_MODULES.split(',');
const stack = new Error().stack;
const filesAt = stack.split('\n');
if (filesAt.length < 4 || !filesAt[3]) {
return true;
}
// 0 -> "Error"
// 1 -> this function
// 2 -> console.log
// 3 -> caller
const callerParts = filesAt[3].split('(');
if (callerParts.length < 2) {
return true;
}
// there will be some line number junk included
const callerFile = callerParts[1];
for (const logModule of logModules) {
if (callerFile.includes(logModule)) {
return true;
}
}
return false;
}
console.log = function() {
if (!checkLogModules()) {
return;
}
return logger.debug.apply(logger, arguments);
};
for (const level of ['debug', 'error', 'info', 'warn']) {
console[level] = function() {
if (!checkLogModules()) {
return;
}
return logger[level].apply(logger, arguments);
};
}
module.exports = logger;