-
Notifications
You must be signed in to change notification settings - Fork 1
/
boscaiolog.js
54 lines (50 loc) · 1.76 KB
/
boscaiolog.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
window.bosco = {
identifier: "default",
v: "0.1.0",
label: "weblogs",
endpoint: "https://qryn.endpoint.com",
log: async function (log_content) {
console.info(log_content) // don't do console.log or you'll create an infinite recursion
window.bosco.send(log_content)
},
init: function (options) {
this.endpoint = options?.endpoint || this.endpoint
this.log = options?.logFunction || this.log
this.getId = options?.idFunction || this.getId
this.label = options?.label || this.label
this.identifier = this.getId()
if(options.consoleOverwrite) {
console.log = this.log
}
if(options.intervalEnabled && options.intervalFn && options.intervalTime > 800) {
setInterval(options.intervalFn, options.intervalTime)
}
},
getId: function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
},
send: async function (log_content, indexed_id) {
let object = {
id: this.identifier,
log: log_content
}
if (!indexed_id) {
var res = await fetch(this.endpoint + "/" + this.label + '/_doc/', {
method: "POST",
body: JSON.stringify(log_content),
}).catch((err)=>{
console.error(err)
})
} else {
var res = await fetch(this.endpoint + "/" + this.label + '/_doc/' + indexed_id, {
method: "PUT",
body: JSON.stringify(log_content)
}).catch((err)=>{
console.error(err)
})
}
}
}