-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.js
47 lines (33 loc) · 1 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
export class Logger {
constructor (elementSelector = '#log', logger = console) {
this.elementSelector = elementSelector;
this.logger = logger;
}
get element () {
if (!this._element) {
this._element = document.querySelector(this.elementSelector);
}
return this._element;
}
log(...args) {
const preTag = document.createElement('pre');
this.logger.log.apply(this.logger, args);
if (!this.element) {
this.logger.error.call(this.logger, 'No element set', this.element)
}
this.element.appendChild(preTag);
preTag.textContent = Array.from(args).map((arg) => JSON.stringify(arg, null, 2)).join(', ');
preTag.classList.add('bg-info');
preTag.style.maxHeight = '25vh';
return preTag;
}
warn(...args) {
const preTag = this.log(...args);
preTag.classList.add('bg-warning');
}
error(...args) {
const preTag = this.log(...args);
preTag.classList.add('bg-danger');
}
}
export const logger = new Logger();