-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (80 loc) · 3.97 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
const fs = require('fs');
const os = require('os');
const colorFunctions = require('./colors');
const rawColors = require('./colors.json');
const colors = colorFunctions.convertColorCodes(rawColors);
function supportsTrueColor() {
return process.env.COLORTERM === 'truecolor' || process.env.TERM_PROGRAM === 'iTerm.app';
}
class SleekLog {
constructor(options = {}) {
this.enableColors = options.enableColors !== false && supportsTrueColor();
this.colors = this.enableColors ? colors : this.stripColors(colors);
this.levels = {
info: { color: this.colors.fg.blue, format: options.format || "[%timestamp%] %message%" },
warn: { color: this.colors.fg.yellow, format: options.format || "[%timestamp%] %message%" },
error: { color: this.colors.fg.red, format: options.format || "[%timestamp%] %message%" },
success: { color: this.colors.fg.green, format: options.format || "[%timestamp%] %message%" },
...options.levels
};
this.logFilePath = options.logFilePath || 'application.log';
this.enableFileLogging = options.enableFileLogging || false;
}
formatTimestamp() {
const now = new Date();
return `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
}
formatMessage(level, message) {
let format = this.levels[level].format;
return format.replace('%timestamp%', this.formatTimestamp()).replace('%message%', message);
}
log(level, message, hexColor = null) {
const ansiColor = hexColor ? (supportsTrueColor() ? colorFunctions.hexToTrueColorAnsi(hexColor) : colorFunctions.hexToAnsi(hexColor)) : this.levels[level] ? this.levels[level].color : this.colors.reset;
const formattedMessage = `${ansiColor}${this.formatMessage(level, message)}${this.colors.reset}`;
console.log(formattedMessage);
if (this.enableFileLogging) {
this.logToFile(`[${this.formatTimestamp()}] ${this.stripAnsi(message)}`);
}
}
stripAnsi(message) {
return message.replace(/\x1b\[[0-9;]*m/g, '');
}
logToFile(message) {
fs.appendFile(this.logFilePath, message + os.EOL, err => {
if (err) console.error('Error writing to log file:', err);
});
}
logError(message, error) {
this.log('error', `${message}\nStack Trace:\n${error.stack}`);
}
drawBar(label, value, maxValue, width, labelColor = this.colors.fg.white, barColor = this.colors.fg.green) {
const percentage = (value / maxValue) * 100;
const barWidth = Math.round((percentage / 100) * width);
const bar = `${barColor}${'█'.repeat(barWidth)}${this.colors.reset}${'░'.repeat(width - barWidth)}`;
this.log('info', `${labelColor}${label}: ${bar} ${percentage.toFixed(2)}%`);
}
startSpinner(duration = 3000, message = 'Loading...', spinnerColor = this.colors.fg.cyan) {
const spinnerFrames = ['⠋', '⠙', '⠚', '⠒', '⠂', '⠂', '⠒', '⠲', '⠴', '⠦', '⠖', '⠒', '⠐', '⠐', '⠒', '⠓', '⠋'];
let i = 0;
const spinner = setInterval(() => {
process.stdout.write(`\r${spinnerColor}${spinnerFrames[i++ % spinnerFrames.length]} ${message}${this.colors.reset}`);
}, 80);
setTimeout(() => {
clearInterval(spinner);
process.stdout.write(`\r${' '.repeat(message.length + 20)}\r`);
console.log('Done!');
}, duration);
}
stripColors(colors) {
const noColor = {};
Object.keys(colors).forEach(key => {
if (typeof colors[key] === 'object') {
noColor[key] = this.stripColors(colors[key]);
} else {
noColor[key] = '';
}
});
return noColor;
}
}
module.exports = SleekLog;