-
Notifications
You must be signed in to change notification settings - Fork 5
/
logger.js
71 lines (60 loc) · 2.05 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
class Logger {
constructor() {
this.levels = ['debug', 'info', 'warn', 'error'];
this.colors = {
debug: '\x1b[90m',
info: '\x1b[94m',
warn: '\x1b[93m',
error: '\x1b[91m',
reset: '\x1b[0m',
};
this.minLogLevel = process.env.LOG_LEVEL || 'debug';
}
setMinLogLevel(level) {
if (this.levels.includes(level)) {
this.minLogLevel = level;
} else {
console.error('Invalid log level. Allowed levels: debug, info, warn, error');
}
}
log(level, message, data) {
const levelIndex = this.levels.indexOf(level);
const minLogLevelIndex = this.levels.indexOf(this.minLogLevel);
if (levelIndex >= minLogLevelIndex) {
const colorCode = this.colors[level] || this.colors.reset;
const callingInfo = this.getCallingInfo();
const formattedMessage = this.formatMessage(level, callingInfo, message, data);
console.log(`${colorCode}${formattedMessage}${this.colors.reset}`);
}
}
getCallingInfo() {
const stack = new Error().stack.split('\n');
const callingLine = stack[4].trim();
if (callingLine.startsWith('at ')) {
const callingInfoMatch = callingLine.match(/at (.+) \((.+):(\d+):(\d+)\)/);
if (callingInfoMatch) {
const [, callingFunction, filename, line] = callingInfoMatch;
return { callingFunction, filename, line };
}
}
return { callingFunction: 'Unknown', filename: 'Unknown', line: 'Unknown' };
}
debug(message, data) {
this.log('debug', message, data);
}
info(message, data) {
this.log('info', message, data);
}
warn(message, data) {
this.log('warn', message, data);
}
error(message, data) {
this.log('error', message, data);
}
formatMessage(level, callingInfo, message, data) {
const timestamp = new Date().toISOString();
const formattedData = data ? JSON.stringify(data) : '';
return `[${timestamp}] [${level.toUpperCase()}] [${callingInfo.callingFunction} - ${callingInfo.filename}:${callingInfo.line}] ${message} ${formattedData}`;
}
}
module.exports = new Logger();