-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-log-file.js
executable file
·55 lines (43 loc) · 1.1 KB
/
watch-log-file.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
const Fs = require('fs')
const Http = require('http')
const siteName = ''
const postPath = '/sites/52266fee-b767-4b04-823c-a24b8d539367/pm2-logs'
const hostName = 'bf5d9465.ngrok.io'
function readLastLines(file, lastXLines, cb) {
const stream = Fs.createReadStream(file, {
flags: 'r',
encoding: 'utf-8',
fd: null,
mode: '0666',
bufferSize: 64 * 1024
})
let fileData = ''
stream.on('data', function(data) {
let lines = data.split('\n')
fileData = lines.splice([lines.length - lastXLines]).join('\n')
})
stream.on('end', function() {
cb(fileData)
})
}
const filePath = '.ex.json'
function sendToNesabox(logs) {
const data = JSON.stringify({
logs
})
const options = {
hostname: hostName,
port: 80,
path: postPath,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
const req = Http.request(options, res => {})
req.write(data)
req.end()
}
Fs.watchFile(filePath, () => {
readLastLines(filePath, 10, sendToNesabox)
})