-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-logs.js
67 lines (53 loc) · 1.57 KB
/
read-logs.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
// const fs = require('fs')
const lineReader = require('line-reader')
const FILE_NAME = './logs/token-liquidity-2020-01-02.log'
function start () {
readLogs(FILE_NAME)
}
start()
// Parent method for reading and displaying log file data.
async function readLogs (filename) {
try {
// const { name } = this.validateFlags(flags)
// this.log(`Opening file ${name}`);
const data = await openLogFile(filename)
for (let i = 0; i < data.length; i++) {
const thisEntry = data[i]
console.log(`${thisEntry.timestamp}: ${thisEntry.message}`)
}
} catch (err) {
console.log('Error in readLogs(): ', err)
}
}
// Opens the Winston log file and returns each line as an array of JSON data.
async function openLogFile (filename) {
try {
// const name = `${__dirname}/../../logs/${name}`
const data = await readLines(filename)
return data
} catch (err) {
console.log('Error in openLogFile()')
throw err
}
}
// Returns an array with each element containing a line of the file.
function readLines (filename) {
return new Promise((resolve, reject) => {
try {
const data = []
// const i = 0
lineReader.eachLine(filename, function (line, last) {
try {
data.push(JSON.parse(line))
// Uncomment to display the raw data in each line of the winston log file.
// console.log(`line ${i}: ${line}`)
// i++
if (last) return resolve(data)
} catch (err) {}
})
} catch (err) {
console.log('Error in readLines()')
return reject(err)
}
})
}