-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
144 lines (126 loc) · 2.83 KB
/
utils.mjs
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
138
139
140
141
142
143
144
function isObject(value) {
return typeof value === 'object' && value !== null;
}
function isString(value) {
return typeof value === 'string';
}
function levelToStatus(level) {
if (level === 10 || level === 20) {
return 'debug';
}
if (level === 40) {
return 'warning';
}
if (level === 50) {
return 'error';
}
if (level >= 60) {
return 'critical';
}
return 'info';
}
function removeFalsy(input) {
if (input === null || input === undefined) {
return input;
}
if (typeof input === 'object' && input !== null) {
let key;
for (key in input) {
const value = input[key];
if (!value) {
delete input[key];
} else if (typeof value === 'object') {
removeFalsy(value);
if (!Object.keys(value).length) {
delete input[key];
}
}
}
}
return input;
}
function prepareLogBackEnd(item) {
const status = levelToStatus(item.level);
const message = item.msg || status;
const host = item.hostname;
const service = item.service;
const pid = item.pid;
const stack = item.stack;
const type = item.type;
const timestamp = item.time || new Date().getTime();
const {
time: _time,
level: _level,
msg: _msg,
hostname: _hostname,
service: _service,
pid: _pid,
stack: _stack,
type: _type,
...cleanedItem
} = item;
const context = removeFalsy({ host, service, pid, stack, type });
return {
metadata: {
...cleanedItem,
context,
level: status
},
message,
timestamp
};
}
function prepareLogFrontEnd(logEvent) {
const {
ts,
messages,
bindings,
level: { value: levelValue }
} = logEvent;
const level = levelToStatus(levelValue);
const timestamp = ts;
const objMessages = messages.filter(isObject);
const strMessages = messages.filter(isString);
const logEntry = strMessages.join(' ');
const defaultMetadata = {
url: window.document.URL,
level: level,
browser: true
};
const bindingsAndMessages = bindings.concat(objMessages);
const metadata = bindingsAndMessages.reduce((acc, el) => {
return Object.assign(acc, el);
}, defaultMetadata);
return {
metadata,
log_entry: logEntry,
timestamp
};
};
async function postLogEvents(fetch, logflare, logEvent) {
const batch = Array.isArray(logEvent) ? logEvent : [logEvent];
try {
const response = await fetch(
`https://api.logflare.app/logs?api_key=${logflare.apiKey}&source=${logflare.sourceToken}`,
{
body: JSON.stringify({
batch
}),
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}
}
);
const data = await response.json();
} catch (e) {
console.log(e);
}
}
export async function sendLogBackEnd(fetch, logflare, obj){
return postLogEvents(fetch, logflare, prepareLogBackEnd(obj))
}
export async function sendLogFrontEnd(fetch, logflare, obj){
return postLogEvents(fetch, logflare, prepareLogFrontEnd(obj))
}