-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
45 lines (37 loc) · 1.17 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
module.exports = class {
constructor(configuration) {
this.configuration = {};
this.configuration.name = configuration.name ?? "Logger";
this.configuration.timestamp = configuration.timestamp ?? true;
}
get timestamp() {
let now = new Date();
let hour = now.getHours();
let minute = now.getMinutes();
let stringHour = hour > 9? hour: `0${hour}`;
let stringMinute = minute > 9? minute: `0${minute}`;
return `[${stringHour}:${stringMinute}]`;
}
get name() {
return `[${this.configuration.name}]`;
}
print(prefix, message) {
if (Array.isArray(message)) {
message.unshift(prefix);
console.log.apply(this, message);
}
console.log.apply(this, [prefix, message]);
}
get logPrefix() {
return this.configuration.timestamp? `${this.timestamp}${this.name}`: this.name;
}
log(message) {
this.print(this.logPrefix, message);
}
error(message) {
this.print(`${this.logPrefix}[ERROR]`, message);
}
warn(message) {
this.print(`${this.logPrefix}[WARN]`, message);
}
}