-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.76 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
const fs = require('fs');
const path = require('path');
class Logger {
constructor(options = {}) {
const { consoleLog = true, filesLog = true, logFolderPath = './logs' } = options;
this.consoleLog = consoleLog;
this.filesLog = filesLog;
this.logFolderPath = logFolderPath;
if (this.filesLog) {
this.ensureLogFolder();
this.logFilePath = this.getLogFilePath();
}
}
getLogFilePath() {
const currentDate = new Date().toLocaleDateString('tr-TR');
return path.join(this.logFolderPath, `${currentDate}.txt`);
}
ensureLogFolder() {
if (!fs.existsSync(this.logFolderPath)) {
fs.mkdirSync(this.logFolderPath, { recursive: true });
}
}
log(message, level) {
if (!message) {
this.warn('Message is required');
return;
}
if (this.consoleLog) {
const color = {
INFO: '\x1b[32m',
ERROR: '\x1b[31m',
WARN: '\x1b[33m',
DEBUG: '\x1b[34m',
};
console.log(`[\x1b[38;2;5;203;247m${new Date().toLocaleTimeString()}\x1b[0m] [${color[level]}${level}\x1b[0m] ${message}`);
}
if (this.filesLog) {
const logMessage = `[${new Date().toLocaleTimeString()}] [${level}] ${message}`;
fs.appendFileSync(this.logFilePath, `${logMessage}\n`);
}
}
info(message) {
this.log(message, 'INFO');
}
error(message) {
this.log(message, 'ERROR');
}
warn(message) {
this.log(message, 'WARN');
}
debug(message) {
this.log(message, 'DEBUG');
}
}
module.exports = Logger;