-
Notifications
You must be signed in to change notification settings - Fork 0
/
VibrantLogger.js
126 lines (114 loc) · 3.64 KB
/
VibrantLogger.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
/**
* VibrantLogger.js
* A simple console logger with visual style for your projects.
* @version 1.1.0
* @author JMcrafter26 <jm26.net>
* @see {@link https://jm26.net}
* @see {@link https://github.com/JMcrafter26/vibrant-logger.js}
* @license MIT
*/
class Logger {
constructor(json) {
if (typeof json !== "object") json = {};
this.name = json.name || "";
this.theme = json.theme || "auto";
var customColors = json.customColors || {};
this.style = json.style || "auto";
this.colorsDark = {
INFO: "#82AAFF",
WARN: "#FFCB6B",
ERROR: "#FF5370",
SUCCESS: "#C3E88D",
DEBUG: "#d382ff",
UNKNOWN: "#abb2bf",
background: "#434C5E"
};
this.colorsLight = {
INFO: "#407dff",
WARN: "#ffb62e",
ERROR: "#FF5370",
SUCCESS: "#a0db48",
DEBUG: "#bc40ff",
UNKNOWN: "#6e7685",
background: "#F5F5F5"
};
if (typeof customColors !== "object") customColors = {};
if (
this.theme !== "dark" &&
this.theme !== "light" &&
this.theme !== "auto" &&
Object.keys(customColors).length === 0
) {
this.colors = this.colorsDark;
} else {
if (this.theme === "auto") {
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: light)").matches
) {
this.theme = "light";
} else {
this.theme = "dark";
}
}
if (this.theme === "dark") this.colors = this.colorsDark;
if (this.theme === "light") this.colors = this.colorsLight;
if (Object.keys(customColors).length !== 0) this.colors = customColors;
}
}
log(message = "No message provided", type = "INFO") {
const typeMsg = type;
if (!this.colors[type]) type = "UNKNOWN";
if (this.name === "time") this.name = new Date().toLocaleTimeString();
var stack = new Error().stack;
var stackArray = stack.split("\n");
// This does not really work, but it's the best I can do
var fileName = stackArray[2].split("/").pop().split(":")[0];
const msgBreak =
(message.length + type.length + this.name.length + 6) * 7 >
window.outerWidth - window.innerWidth - fileName.length - 180;
var base;
if (this.style === "maxi" && !msgBreak) {
if (this.name === "") {
base = "%c " + typeMsg + "\n%c " + message + " ";
} else {
base = "%c " + this.name + " " + typeMsg + "\n%c " + message + " ";
}
} else {
if (this.name === "") {
base = "%c " + typeMsg + " %c " + message + " ";
} else {
base = "%c " + this.name + " " + typeMsg + " %c " + message + " ";
}
}
var style1;
var style2;
if (!msgBreak && this.style === "auto") {
style1 = `background: ${this.colors[type]}; color: ${this.colors.background}; padding: 1px; border-radius: 3px 0 0 3px;`;
style2 = `background: ${this.colors.background}; color: ${this.colors[type]}; padding: 1px; border-radius: 0 3px 3px 0;`;
} else if (msgBreak || this.style === "maxi") {
style1 = `background: ${this.colors[type]}; color: ${this.colors.background}; padding: 1px; border-radius: 3px 3px 0 0;`;
style2 = `background: ${this.colors.background}; color: ${this.colors[type]}; padding: 1px; border-radius: 0 3px 3px 3px;`;
}
// log the message
console.log(base, style1, style2);
}
info(message) {
this.log(message, "INFO");
}
warn(message) {
this.log(message, "WARN");
}
error(message) {
this.log(message, "ERROR");
}
success(message) {
this.log(message, "SUCCESS");
}
debug(message) {
this.log(message, "DEBUG");
}
unknown(message) {
this.log(message, "UNKNOWN");
}
}