-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-LogExt.js
137 lines (123 loc) · 4.31 KB
/
MMM-LogExt.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
137
"use strict"
Module.register("MMM-LogExt", {
defaults: {
echo: true,
notificationMonitor: (notification, payload, senderName) => {
return ''
},
beforeContext: ({method, location}) => {
switch (method) {
case 'error': return `\x1b[1;31m[${location}]\x1b[0m`
default: return `\x1b[1m[${location}]\x1b[0m`
}
},
afterContext: () => {
return ''
},
replaceJSON: (key, value) => {
return value
}
},
fallbacks: {
notificationMonitor: () => {
return ''
},
beforeContext: () => {
return ''
},
afterContext: () => {
return ''
},
replaceJSON: (key, value) => {
return value
}
},
notificationReceived: function (noti, pl, sender) {
const msg = ((typeof this.config.notificationMonitor === 'function') ? this.config.notificationMonitor : this.fallbacks.notificationMonitor)(noti, pl, (sender?.name ?? ''))
if (msg) {
let context = ['[NOTIFICATION]', msg]
this.send({method: 'log', context})
if (this.config.echo) console.log(...context)
}
},
start: function() {
if (!navigator.sendBeacon) {
Log.warn("MMM-LogExt: navigator.sendBeacon is not supported in this browser.")
// return
}
/* reserved for later use : Multi-screen/device support.
const hashCode = (s) => {
return [...s].reduce(
(hash, c) => (Math.imul(31, hash) + c.charCodeAt(0)) | 0,
0
)
}
*/
const beforeContext = (typeof this.config.beforeContext === 'function') ? this.config.beforeContext : this.fallbacks.beforeContext
const afterContext = (typeof this.config.afterContext === 'function') ? this.config.afterContext : this.fallbacks.afterContext
const filtered = (value) => {
return (value) ? [value] : []
}
window.onerror = (msg, url, lineNo, columnNo, error) => {
const location = `${url.split('/').at(-1)}:${lineNo}:${columnNo}`
const context = [
'[ERROR]',
...filtered(beforeContext({method: 'error', location, stack: error.stack, context:msg})),
msg,
...filtered(afterContext({method: 'error', location, stack: error.stack, context:msg}))
]
this.send({method: 'error', context})
if (this.config.echo) console.error(error.stack)
return false
}
window.onunhandledrejection = (event) => {
const stack = event.reason.stack.split('\n')
const msg = stack[0]
const location = `${stack[1].split('/').at(-1)}`.replaceAll(')', '')
const context = [
'[UNHANDLED REJECTION]',
...filtered(beforeContext({method: 'error', location, stack, context:msg})),
msg,
...filtered(afterContext({method: 'error', location, stack, context:msg}))
]
this.send({method: 'error', context})
if (this.config.echo) console.error(event.reason.stack)
return false
}
const logLevel = config.logLevel
for (let method of Object.keys(Log)) {
if (!logLevel.includes(method.toLocaleUpperCase())) continue
Log[method] = Function.prototype.bind.call((...args) => {
let stack = new Error().stack
stack = stack.split('\n').slice(2)
let location = stack[0].trim().split('/').at(-1).replaceAll(')', '')
let context = [
...filtered(beforeContext({method, location, stack, context:args})),
...args,
...filtered(afterContext({method, location, stack, context:args}))
]
this.send({method, context})
if (this.config.echo) console[method](...context)
}, console)
}
Log.log("MMM-LogExt hooks original Log from now on.")
//let userAgent = navigator.userAgent
//Log.info(`Broswer ID : ${hashCode(userAgent)} for ${userAgent}`) // Reserved for later use : Multi-screen/device support.
},
send: function (dataObj) {
// use navigator.sendBeacon if available, fallback is fetch with keepalive option
const replaceJSON = (typeof this.config.replaceJSON === 'function') ? this.config.replaceJSON : this.fallbacks.replaceJSON
const url = '/logext'
const payload = JSON.stringify(dataObj, replaceJSON)
if (navigator.sendBeacon) {
navigator.sendBeacon(url, payload)
}
else {
fetch(url, {
method: 'POST',
body: payload,
keepalive: true
})
}
}
})