-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (52 loc) · 2.1 KB
/
index.js
File metadata and controls
62 lines (52 loc) · 2.1 KB
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
// Do not process anything if the env is develop
const trueValues = [true, 'true', 'TRUE'];
if (process.env.VERBOSE && trueValues.includes(process.env.VERBOSE)) {
return;
}
const path = require('path');
const { inspect } = require('util');
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf, colorize } = format;
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageObj = require(packageJsonPath);
const appName = packageObj.name || 'app';
const SPLAT = Symbol.for('splat');
// copied from https://github.com/chalk/ansi-regex/blob/main/index.js
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
].join('|');
const ansiRegex = new RegExp(pattern);
function isPrimitive(val) {
return val === null || (typeof val !== 'object' && typeof val !== 'function');
}
function formatWithInspect(val) {
const prefix = isPrimitive(val) ? '' : '\n';
const shouldFormat = typeof val !== 'string' && !ansiRegex.test(val);
return prefix + (shouldFormat ? inspect(val, { depth: null, colors: true }) : val);
}
// Custom format of the logs
const generateItFormat = printf((info) => {
const msg = formatWithInspect(info.message);
const splatArgs = info[SPLAT] || [];
const rest = splatArgs.map((data) => formatWithInspect(data)).join(' ');
return `[${info.label}] ${info.timestamp} ${info.level}: ${msg} ${rest}`;
});
// Custom logging handler
const logger = createLogger({
format: combine(colorize(), label({ label: appName }), timestamp(), generateItFormat),
transports: [new transports.Console()],
});
// Override the base console log with winston
console.log = function (...args) {
return logger.info.apply(logger, [...args]);
};
console.error = function (...args) {
return logger.error.apply(logger, [...args]);
};
console.warn = function (...args) {
return logger.warn.apply(logger, [...args]);
};
console.info = function (...args) {
return logger.info.apply(logger, [...args]);
};