-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.js
76 lines (65 loc) · 1.57 KB
/
log.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
const config = require('./config.json')
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 = config.logging_server
// 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)
})
}
const begin_heartbeat = async function(period){
while(true){
await pause(period)
log.info('HEARTBEAT')
}
}
// // 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({
// data: data,
// })
// }).catch(err => console.log(err))
// await request
// }
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({data: chunk.toString('utf8')})
}).catch(err => console.log(err))
next()
}
})
const log = bunyan.createLogger({
name: "now-playing",
streams: [
{
level: 'debug',
type: 'raw',
stream: out_pretty
},
{
level: 'info',
type: 'stream',
stream: remote
}
]
})
module.exports = log
begin_heartbeat(5)