-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathbetterLogging.js
36 lines (32 loc) · 1.05 KB
/
betterLogging.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
rewireLoggingToElement(
() => document.getElementById("log"),
() => document.getElementById("log-container"),
true
);
function rewireLoggingToElement(eleLocator, eleOverflowLocator, autoScroll) {
fixLoggingFunc("log");
function fixLoggingFunc(name) {
console.old = console.log;
console.log = function (...arg) {
const output = produceOutput(name, arg);
const eleLog = eleLocator();
if (autoScroll) {
const eleContainerLog = eleOverflowLocator();
const isScrolledToBottom =
eleContainerLog.scrollHeight - eleContainerLog.clientHeight <=
eleContainerLog.scrollTop + 1;
eleLog.innerHTML += output + "<br>";
if (isScrolledToBottom) {
eleContainerLog.scrollTop =
eleContainerLog.scrollHeight - eleContainerLog.clientHeight;
}
} else {
eleLog.innerHTML += output + "<br>";
}
};
}
function produceOutput(name, args) {
arg = args[0].replace("%c", "");
return '<span style="' + args[1] + '">' + arg + "</span> ";
}
}