This repository was archived by the owner on Feb 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLogger.js
More file actions
83 lines (64 loc) · 2.21 KB
/
Logger.js
File metadata and controls
83 lines (64 loc) · 2.21 KB
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
const TimeStamp = require("time-stamp");
const TextFormat = pocketnode("utils/TextFormat");
const TerminalTextFormat = pocketnode("utils/TerminalTextFormat");
class Logger {
constructor(caller, subcaller = ""){
this.debuggingLevel = 0;
this.caller = caller;
this.subcaller = subcaller !== "" ? " " + subcaller : "";
}
emergency(){
return this.log("Emergency", arguments, TerminalTextFormat.RED);
}
alert(){
return this.log("Alert", arguments, TerminalTextFormat.RED);
}
critical(){
return this.log("Critical", arguments, TerminalTextFormat.RED);
}
error(){
return this.log("Error", arguments, TerminalTextFormat.DARK_RED);
}
warning(){
return this.log("Warning", arguments, TerminalTextFormat.YELLOW);
}
notice(){
return this.log("Notice", arguments, TerminalTextFormat.AQUA);
}
info(){
return this.log("Info", arguments, TerminalTextFormat.WHITE);
}
debug(){
if(this.debuggingLevel < 1) return;
return this.log("Debug", arguments, TerminalTextFormat.GRAY);
}
debugExtensive(){
if(this.debuggingLevel < 2) return;
return this.log("Debug", arguments, TerminalTextFormat.GRAY);
}
logError(error){
error = error.stack.split("\n");
this.error(error.shift());
error.forEach(trace => this.debug(trace));
}
/**
* @param level String
* @param messages Array
* @param color TerminalTextFormat.COLOR
*/
log(level, messages, color = TerminalTextFormat.GRAY){
if(messages.length === 0) return;
messages = Array.from(messages).map(message => {
return (typeof message === "string" ? TextFormat.toTerminal(message) + TerminalTextFormat.RESET : message);
});
log(TerminalTextFormat.BLUE + "[" + TimeStamp("HH:mm:ss") + "]" + TerminalTextFormat.RESET + " " + color +"[" + this.caller + " > " + level + "]:" + this.subcaller, messages);
function log(prefix, args){
console.log.apply(this, [prefix].concat(args));
}
}
setDebugging(level){
this.debuggingLevel = level;
return this;
}
}
module.exports = Logger;