-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.js
More file actions
81 lines (71 loc) · 1.63 KB
/
log.js
File metadata and controls
81 lines (71 loc) · 1.63 KB
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
const bunyan = require('bunyan')
const prettyStream = require('bunyan-prettystream')
const stream = require('stream')
const fetch = require('node-fetch')
const out_pretty = new prettyStream
out_pretty.pipe(process.stdout)
const remote_url = "http://45.55.38.183:4002/log"
// Utility function, returns a promise that
// resolves after the given number of seconds
const pause = function(duration) {
return new Promise(function(resolve, reject){
setTimeout(resolve, duration * 1000)
})
}
// Sends a post request to the remote_url with the given data
const write_remote = async function(event, data){
const request = fetch(remote_url, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({
event: event,
data: data,
sender: "(N)EAL"
})
}).catch(err => console.log('heartbeat failed to send'))
await request
}
const begin_heartbeat = async function(period){
while(true){
await pause(period)
console.log("sending heartbeat")
await write_remote('HEARTBEAT')
}
}
const remote = new stream.Writable({
write: async function(chunk, encoding, next){
fetch(remote_url, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({
event: "LOG",
data: chunk.toString('utf8'),
sender: "(N)EAL"
})
}).catch(err => {})
next()
}
})
const log = bunyan.createLogger({
name: "neal",
streams: [
{
level: 'debug',
type: 'raw',
stream: out_pretty
},
{
level: 'info',
type: 'stream',
stream: remote
}
]
})
begin_heartbeat(5);
module.exports = log