-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (104 loc) · 3.54 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
/* eslint-disable security/detect-object-injection */
const fs = require('fs');
const path = require('path');
class Logger {
// Requires Node v12+
// static COLORS = {
// ERROR: '\x1b[31m%s', // Red
// WARN: '\x1b[33m%s', // Yellow
// SUCCESS: '\x1b[32m%s', // Green
// INFO: '\x1b[37m%s', // White
// DEBUG: '\x1b[36m%s', // Cyan
// CLEAR: '\x1b[0m' // Clear
// };
constructor(config = { levels: ['ERROR', 'WARN', 'SUCCESS', 'INFO', 'DEBUG'] }) {
this.COLORS = {
ERROR: '\x1b[31m%s', // Red
WARN: '\x1b[33m%s', // Yellow
SUCCESS: '\x1b[32m%s', // Green
INFO: '\x1b[37m%s', // White
DEBUG: '\x1b[36m%s', // Cyan
CLEAR: '\x1b[0m' // Clear
};
this.label = config.label || '';
this.timestampStr = () => {
switch (config.timestamp) {
case 'iso':
return (new Date()).toISOString();
case 'locale':
return (new Date()).toLocaleString();
default:
return Logger.toCLFDateTimeString();
}
};
if (config.console === 'file' || config.console === 'both') {
try {
// eslint-disable-next-line no-bitwise
fs.accessSync(path.dirname(config.file), fs.constants.F_OK | fs.constants.W_OK);
const mode = config.append ? 'a' : 'w';
// eslint-disable-next-line security/detect-non-literal-fs-filename
const logFile = fs.createWriteStream(config.file, { flags: mode });
if (config.console === 'both') { // file & console
this.log = (level, ...msg) => {
if (config.levels.includes(level)) {
logFile.write(`${[level, ...msg].join(' ')}\n`);
// eslint-disable-next-line no-console
console.log(this.COLORS[level], ...msg, this.COLORS.CLEAR);
}
};
}
else { // file only
this.log = (level, ...msg) => {
if (config.levels.includes(level)) {
logFile.write(`${[level, ...msg].join(' ')}\n`);
}
};
}
} catch (err) {
// eslint-disable-next-line no-console
console.log('Log directory is not writable: ', err);
throw new Error('Log directory is not writable');
}
} else { // console only
this.log = (level, ...msg) => {
if (config.levels.includes(level)) {
// eslint-disable-next-line no-console
console.log(this.COLORS[level], ...msg, this.COLORS.CLEAR);
}
};
}
}
error(...msg) {
this.log('ERROR', this.timestampStr(), this.label, ...msg);
}
warn(...msg) {
this.log('WARN', this.timestampStr(), this.label, ...msg);
}
success(...msg) {
this.log('SUCCESS', this.timestampStr(), this.label, ...msg);
}
info(...msg) {
this.log('INFO', this.timestampStr(), this.label, ...msg);
}
debug(...msg) {
this.log('DEBUG', this.timestampStr(), this.label, ...msg);
}
static toCLFDateTimeString(dateTime = new Date()) {
const pad2 = (n) => {
const str = n.toString();
return (str.length === 1 ? '0' : '') + str;
};
const MONTH3 = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
];
const date = dateTime.getUTCDate();
const hour = dateTime.getUTCHours();
const mins = dateTime.getUTCMinutes();
const secs = dateTime.getUTCSeconds();
const year = dateTime.getUTCFullYear();
const month = MONTH3[dateTime.getUTCMonth()];
return (`${pad2(date)}/${month}/${year}:${pad2(hour)}:${pad2(mins)}:${pad2(secs)} +0000`);
}
}
module.exports=Logger;