-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logger.js
136 lines (126 loc) · 3.79 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const Colors = {
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
cyan: "\x1b[34m",
purple: "\x1b[35m",
blue: "\x1b[36m",
white: "\x1b[37m",
default: "\x1b[0m"
}
const BgColors = {
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
cyan: "\x1b[44m",
purple: "\x1b[45m",
blue: "\x1b[46m",
white: "\x1b[47m",
default: "\x1b[0m"
}
class Logger {
constructor() {
/**
* @type {string[]}
*/
this.logarray = [];
/**
* @type {Colors}
*/
this.colors = Colors;
/**
* @type {BgColors}
*/
this.bgcolors = BgColors;
}
/**
* @param {String} a
* @param {String} b
* @param {{TitleColor: "red"|"green"|"yellow"|"cyan"|"purple"|"blue"|"white"|"default", TextColor: "red"|"green"|"yellow"|"cyan"|"purple"|"blue"|"white"|"default"}} options
* @type {Function}
*/
log(a, b, options = {}) {
this.sendtimelog(this.glue(` ${a}: `, Colors[options.TitleColor || 'purple']), this.glue(b, Colors[options.TextColor || 'white']))
}
/**
* @param {String} a
* @param {String} b
* @param {{TitleColor: "red"|"green"|"yellow"|"cyan"|"purple"|"blue"|"white"|"default", TextColor: "red"|"green"|"yellow"|"cyan"|"purple"|"blue"|"white"|"default"}} options
* @type {Function}
*/
errorLog(a, b, options = {}) {
this.sendtimelog(this.glue(` ${a}: `, Colors[options.TitleColor || 'purple']), this.glue(b, Colors[options.TextColor || 'white']))
}
glue() {
var ar = [];
for (var i = 0; i < arguments.length; i++) {
ar.push(arguments[i])
}
var txt = ar.shift();
return ar.join("") + txt + "\x1b[0m"
}
sendtimelog() {
var s = '',
d = new Date();
try {
for (var i = 0; i < arguments.length; i++) {
s += arguments[i]
}
console.log(`\x1b[36m[${d.today()} ${d.timeNow()}]: \x1b[0m` + s)
if (this.logarray.length >= 70) {
this.logarray.shift()
}
this.logarray.push(
JSON.stringify(JSON.parse(`{"time":"${d.today()}-${d.timeNow()}","message":"${s.replace(/\[[0-9]{1,2}m/g,"").replace(/\n/g," ").toString()}"}`))
);
return `[${d.today()} ${d.timeNow()}]: ${s}`;
} catch (e){
}
}
/**
* @param {String} t
* @param {"red"|"green"|"yellow"|"cyan"|"purple"|"blue"|"white"|"default"} renk
*/
setRenk(t, renk) {
return this.glue(t, this.colors[renk]);
}
send() {
var s = '',
d = new Date();
try {
for (var i = 0; i < arguments.length; i++) {
s += arguments[i]
}
console.log(s + this.colors.default);
if (this.logarray.length >= 70) {
this.logarray.shift()
}
this.logarray.push(JSON.parse(`{"time":"${d.today()}-${d.timeNow()}","message":"${s.replace(/\[[0-9]{1,2}m/g,"").replace(/\n/g,"").toString()}"}`))
return s;
} catch {}
}
logs() {
return this.logarray
}
};
Logger.prototype.bgcolors = {
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
cyan: "\x1b[44m",
purple: "\x1b[45m",
blue: "\x1b[46m",
white: "\x1b[47m",
default: "\x1b[0m"
}
Logger.prototype.colors = {
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
cyan: "\x1b[34m",
purple: "\x1b[35m",
blue: "\x1b[36m",
white: "\x1b[37m",
default: "\x1b[0m"
}
module.exports = Logger;