forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-event.js
44 lines (39 loc) · 1.01 KB
/
log-event.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
const Node = { http: require('http') };
let Events = [];
let Timeout = undefined;
function PostEvents() {
const lines = Events.join('\n');
Events = [];
Timeout = undefined;
const options = {
method: 'POST',
host: LogEvent.HOST,
port: LogEvent.PORT,
path: '/',
headers: {}
};
const request = Node.http.request(options, function(response) {});
request.on('error', function(error) {}); // Silence exceptions.
request.write(lines);
request.end();
}
function LogEvent(object) {
if (!LogEvent.ENABLED) return;
if (!LogEvent.HOST || !LogEvent.PORT) {
throw new Error('LogEvent.HOST and PORT must be declared.');
}
if (typeof object === 'string') {
const timestamp = Date.now();
object = {
start: timestamp,
end: timestamp,
label: object
};
}
Events.push(JSON.stringify(object));
if (Timeout === undefined) Timeout = setTimeout(PostEvents, 200);
}
LogEvent.ENABLED = true;
LogEvent.HOST = undefined;
LogEvent.PORT = undefined;
module.exports = LogEvent;