mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 09:11:59 -07:00
Improved log handling
This commit is contained in:
Vendored
+2
-1
@@ -4,5 +4,6 @@
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
port: process.env.PORT || 5001
|
||||
port: process.env.PORT || 5001,
|
||||
logPass: 'test'
|
||||
}
|
||||
|
||||
@@ -63,9 +63,9 @@
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
<tr class="tableTemplate">
|
||||
<td>a</td>
|
||||
<td>b</td>
|
||||
<td>c</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
@@ -130,10 +130,13 @@
|
||||
|
||||
cols.first().text(time.toLocaleString())
|
||||
cols.next().text(logData[i].level)
|
||||
cols.next().text(logData[i].message)
|
||||
cols.next().next().text(logData[i].message)
|
||||
// debugger
|
||||
|
||||
$('.table').append(thisRow)
|
||||
}
|
||||
|
||||
|
||||
// } else {
|
||||
// console.log(`password fail`)
|
||||
// }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
const lineReader = require('line-reader')
|
||||
const fs = require('fs')
|
||||
|
||||
const config = require('../../../config')
|
||||
|
||||
let _this
|
||||
|
||||
class LogsApi {
|
||||
@@ -46,35 +48,31 @@ class LogsApi {
|
||||
*/
|
||||
async getLogs (ctx) {
|
||||
try {
|
||||
console.log('entering getLogs()')
|
||||
// console.log('entering getLogs()')
|
||||
|
||||
// Get the user-provided password.
|
||||
const password = ctx.request.body.password
|
||||
_this.password = password
|
||||
console.log(`password: ${password}`)
|
||||
// console.log(`password: ${password}`)
|
||||
|
||||
if (password === 'test') {
|
||||
const now = new Date()
|
||||
const thisDate = now.getDate()
|
||||
// Password matches the password set in the config file.
|
||||
if (password === config.logPass) {
|
||||
// Generate the full path and file name for the current log file.
|
||||
const fullPath = _this.generateFileName()
|
||||
|
||||
let thisMonth = now.getMonth() + 1
|
||||
thisMonth = ('0' + thisMonth).slice(-2)
|
||||
// console.log(`thisMonth: ${thisMonth}`)
|
||||
|
||||
const filename = `koa-dev-2020-${thisMonth}-${thisDate}.log`
|
||||
// console.log(`filename: ${filename}`)
|
||||
const logDir = `${__dirname}/../../../logs/`
|
||||
const fullPath = `${logDir}${filename}`
|
||||
// console.log(`fullPath: ${fullPath}`)
|
||||
|
||||
// const data = require(`${__dirname}/../../../logs/${filename}`)
|
||||
// const data = await this.readFile(filename)
|
||||
// Read in the data from the log file.
|
||||
const data = await _this.readLines(fullPath)
|
||||
// console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
|
||||
// Filter the logs before passing them to the front end.
|
||||
const filteredData = _this.filterLogs(data)
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
data
|
||||
data: filteredData
|
||||
}
|
||||
|
||||
// Password does not match password in config file.
|
||||
} else {
|
||||
ctx.body = {
|
||||
success: false
|
||||
@@ -90,6 +88,61 @@ class LogsApi {
|
||||
}
|
||||
}
|
||||
|
||||
// Sorts the log data by their timestamp. Returns the LIMIT or less elements.
|
||||
filterLogs (data) {
|
||||
try {
|
||||
// console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
|
||||
const LIMIT = 100 // Max number of entries to return.
|
||||
|
||||
// Sort the elements by date.
|
||||
data.sort(function (a, b) {
|
||||
let dateA = new Date(a.timestamp)
|
||||
dateA = dateA.getTime()
|
||||
|
||||
let dateB = new Date(b.timestamp)
|
||||
dateB = dateB.getTime()
|
||||
|
||||
return dateB - dateA
|
||||
})
|
||||
|
||||
// Limit the number of elements.
|
||||
if (data.length > LIMIT) {
|
||||
return data.slice(0, LIMIT)
|
||||
}
|
||||
|
||||
// else
|
||||
return data
|
||||
} catch (err) {
|
||||
console.error('Error in logapi/controller.js/filterLogs()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
generateFileName () {
|
||||
try {
|
||||
const now = new Date()
|
||||
const thisDate = now.getDate()
|
||||
|
||||
let thisMonth = now.getMonth() + 1
|
||||
thisMonth = ('0' + thisMonth).slice(-2)
|
||||
// console.log(`thisMonth: ${thisMonth}`)
|
||||
|
||||
const thisYear = now.getFullYear()
|
||||
|
||||
const filename = `koa-${config.env}-${thisYear}-${thisMonth}-${thisDate}.log`
|
||||
// console.log(`filename: ${filename}`)
|
||||
const logDir = `${__dirname}/../../../logs/`
|
||||
const fullPath = `${logDir}${filename}`
|
||||
// console.log(`fullPath: ${fullPath}`)
|
||||
|
||||
return fullPath
|
||||
} catch (err) {
|
||||
console.error('Error in logapi/controller.js/generateFileName()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Promise based read-file
|
||||
readFile (path, opts = 'utf8') {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -105,7 +158,7 @@ class LogsApi {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const data = []
|
||||
let i = 0
|
||||
// let i = 0
|
||||
|
||||
lineReader.eachLine(filename, function (line, last) {
|
||||
try {
|
||||
@@ -113,7 +166,7 @@ class LogsApi {
|
||||
|
||||
// Uncomment to display the raw data in each line of the winston log file.
|
||||
// console.log(`line ${i}: ${line}`)
|
||||
i++
|
||||
// i++
|
||||
|
||||
if (last) return resolve(data)
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user