diff --git a/config/env/common.js b/config/env/common.js index 72c6751..28a73a0 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -4,5 +4,6 @@ */ module.exports = { - port: process.env.PORT || 5001 + port: process.env.PORT || 5001, + logPass: 'test' } diff --git a/config/logs/index.html b/config/logs/index.html index 1085139..ae363df 100644 --- a/config/logs/index.html +++ b/config/logs/index.html @@ -63,9 +63,9 @@ Message - a - b - c + + + @@ -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`) // } diff --git a/src/modules/logapi/controller.js b/src/modules/logapi/controller.js index 6d67147..4ec7377 100644 --- a/src/modules/logapi/controller.js +++ b/src/modules/logapi/controller.js @@ -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) {