2021-06-01 12:54:47 +00:00
|
|
|
class Logger {
|
2021-06-22 12:49:00 +00:00
|
|
|
log(message, level = 'INFO') {
|
|
|
|
console.log(`[${this.generateTimestamp()}] [${level}] ${message}`)
|
2021-06-01 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
generateTimestamp() {
|
|
|
|
const d = new Date();
|
2021-06-01 12:54:47 +00:00
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
// Date
|
|
|
|
const year = d.getFullYear();
|
|
|
|
const month = this.parseDate(d.getMonth() + 1);
|
|
|
|
const day = this.parseDate(d.getDate());
|
2021-06-01 12:54:47 +00:00
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
// Time
|
|
|
|
const hour = this.parseDate(d.getHours());
|
|
|
|
const minutes = this.parseDate(d.getMinutes());
|
|
|
|
const seconds = this.parseDate(d.getSeconds());
|
|
|
|
const miliseconds = this.parseDate(d.getMilliseconds(), true);
|
2021-06-01 12:54:47 +00:00
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
// Timezone
|
|
|
|
const tz = -d.getTimezoneOffset() / 60;
|
|
|
|
|
2021-06-23 12:15:14 +00:00
|
|
|
return `${year}-${month}-${day} ${hour}:${minutes}:${seconds}.${miliseconds} UTC${tz >= 0 ? '+' + tz : tz}`;
|
2021-06-01 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
parseDate(date, ms = false) {
|
|
|
|
if (ms) {
|
|
|
|
if (date >= 10 && date < 100) {
|
|
|
|
return `0${date}`;
|
|
|
|
} else if (date < 10) {
|
|
|
|
return `00${date}`;
|
|
|
|
}
|
2021-06-01 12:54:47 +00:00
|
|
|
}
|
2021-06-22 12:49:00 +00:00
|
|
|
|
|
|
|
return date < 10 ? `0${date}` : date.toString();
|
2021-06-01 12:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
module.exports = Logger;
|