-
Logger
+
+
Logger
+
+
+
+
@@ -22,7 +26,6 @@
No Logs
-
Log listening starts when you login
@@ -38,6 +41,9 @@ export default {
},
data() {
return {
+ search: null,
+ searchTimeout: null,
+ searchText: null,
newServerSettings: {},
logColors: ['yellow-200', 'gray-400', 'info', 'warning', 'error', 'red-800', 'blue-400'],
logLevels: [
@@ -53,7 +59,8 @@ export default {
value: 3,
text: 'Warn'
}
- ]
+ ],
+ loadedLogs: []
}
},
watch: {
@@ -73,8 +80,14 @@ export default {
return this.logLevels
},
logs() {
- return this.$store.state.logs.logs.filter((log) => {
- return log.level >= this.newServerSettings.logLevel
+ return this.loadedLogs.filter((log) => {
+ if (log.level >= this.newServerSettings.logLevel) {
+ if (this.searchText) {
+ return log.message.toLowerCase().includes(this.searchText)
+ }
+ return true
+ }
+ return false
})
},
serverSettings() {
@@ -85,6 +98,16 @@ export default {
}
},
methods: {
+ inputUpdate() {
+ clearTimeout(this.searchTimeout)
+ this.searchTimeout = setTimeout(() => {
+ if (!this.search || !this.search.trim()) {
+ this.searchText = ''
+ return
+ }
+ this.searchText = this.search.toLowerCase().trim()
+ }, 500)
+ },
updateScroll() {
if (this.$refs.container) {
this.$refs.container.scrollTop = this.$refs.container.scrollHeight - this.$refs.container.clientHeight
@@ -96,7 +119,7 @@ export default {
}
this.updateServerSettings(payload)
- this.$store.dispatch('logs/setLogListener', this.newServerSettings.logLevel)
+ this.$root.socket.emit('set_log_listener', this.newServerSettings.logLevel)
this.$nextTick(this.updateScroll)
},
updateServerSettings(payload) {
@@ -109,6 +132,14 @@ export default {
console.error('Failed to update server settings', error)
})
},
+ logEvtReceived(payload) {
+ this.loadedLogs.push(payload)
+
+ // Dont let logs get too large
+ if (this.loadedLogs.length > 5050) {
+ this.loadedLogs = this.loadedLogs.slice(-5000)
+ }
+ },
init(attempts = 0) {
if (!this.$root.socket) {
if (attempts > 10) {
@@ -119,7 +150,15 @@ export default {
}, 250)
return
}
+
this.newServerSettings = this.serverSettings ? { ...this.serverSettings } : {}
+ this.$root.socket.on('daily_logs', this.dailyLogsLoaded)
+ this.$root.socket.on('log', this.logEvtReceived)
+ this.$root.socket.emit('set_log_listener', this.newServerSettings.logLevel)
+ this.$root.socket.emit('fetch_daily_logs')
+ },
+ dailyLogsLoaded(lines) {
+ this.loadedLogs = lines
}
},
updated() {
@@ -127,6 +166,11 @@ export default {
},
mounted() {
this.init()
+ },
+ beforeDestroy() {
+ if (!this.$root.socket) return
+ this.$root.socket.off('daily_logs', this.dailyLogsLoaded)
+ this.$root.socket.off('log', this.logEvtReceived)
}
}
diff --git a/client/store/logs.js b/client/store/logs.js
deleted file mode 100644
index 4f0e7c03..00000000
--- a/client/store/logs.js
+++ /dev/null
@@ -1,31 +0,0 @@
-export const state = () => ({
- isListening: false,
- logs: []
-})
-
-export const getters = {
-
-}
-
-export const actions = {
- setLogListener({ state, commit, dispatch }) {
- dispatch('$nuxtSocket/emit', {
- label: 'main',
- evt: 'set_log_listener',
- msg: 0
- }, { root: true })
- commit('setIsListening', true)
- }
-}
-
-export const mutations = {
- setIsListening(state, val) {
- state.isListening = val
- },
- logEvt(state, payload) {
- state.logs.push(payload)
- if (state.logs.length > 500) {
- state.logs = state.logs.slice(50)
- }
- }
-}
\ No newline at end of file
diff --git a/server/LogManager.js b/server/LogManager.js
index b7590d51..fe684f45 100644
--- a/server/LogManager.js
+++ b/server/LogManager.js
@@ -4,7 +4,6 @@ const fs = require('fs-extra')
const DailyLog = require('./objects/DailyLog')
const Logger = require('./Logger')
-const { getFileSize } = require('./utils/fileUtils')
const TAG = '[LogManager]'
@@ -50,14 +49,16 @@ class LogManager {
if (this.dailyLogFiles.includes(currentDailyLogFilename)) {
Logger.debug(TAG, `Daily log file already exists - set in Logger`)
- this.currentDailyLog.loadLogs()
+ await this.currentDailyLog.loadLogs()
} else {
this.dailyLogFiles.push(this.currentDailyLog.filename)
}
// Log buffered Logs
if (this.dailyLogBuffer.length) {
- this.dailyLogBuffer.forEach((logObj) => this.currentDailyLog.appendLog(logObj))
+ this.dailyLogBuffer.forEach((logObj) => {
+ this.currentDailyLog.appendLog(logObj)
+ })
this.dailyLogBuffer = []
}
}
@@ -68,7 +69,7 @@ class LogManager {
if (dailyFiles && dailyFiles.length) {
dailyFiles.forEach((logFile) => {
if (Path.extname(logFile) === '.txt') {
- Logger.info('Daily Log file found', logFile)
+ Logger.debug('Daily Log file found', logFile)
this.dailyLogFiles.push(logFile)
} else {
Logger.debug(TAG, 'Unknown File in Daily log files dir', logFile)
@@ -120,5 +121,14 @@ class LogManager {
// Append log line to log file
this.currentDailyLog.appendLog(logObj)
}
+
+ socketRequestDailyLogs(socket) {
+ if (!this.currentDailyLog) {
+ return
+ }
+
+ var lastLogs = this.currentDailyLog.logs.slice(-5000)
+ socket.emit('daily_logs', lastLogs)
+ }
}
module.exports = LogManager
\ No newline at end of file
diff --git a/server/Logger.js b/server/Logger.js
index e27fbfa8..f8c83da0 100644
--- a/server/Logger.js
+++ b/server/Logger.js
@@ -3,7 +3,7 @@ const { LogLevel } = require('./utils/constants')
class Logger {
constructor() {
this.logLevel = process.env.NODE_ENV === 'production' ? LogLevel.INFO : LogLevel.TRACE
- this.logFileLevel = LogLevel.INFO
+ // this.logFileLevel = LogLevel.INFO
this.socketListeners = []
this.logManager = null
@@ -60,7 +60,7 @@ class Logger {
level
}
- if (level >= this.logFileLevel && this.logManager) {
+ if (level >= this.logLevel && this.logManager) {
this.logManager.logToFile(logObj)
}
diff --git a/server/Server.js b/server/Server.js
index 6158b424..c8afc7d9 100644
--- a/server/Server.js
+++ b/server/Server.js
@@ -260,6 +260,7 @@ class Server {
// Logs
socket.on('set_log_listener', (level) => Logger.addSocketListener(socket, level))
+ socket.on('fetch_daily_logs', () => this.logManager.socketRequestDailyLogs(socket))
// Backups
socket.on('create_backup', () => this.backupManager.requestCreateBackup(socket))
diff --git a/server/objects/DailyLog.js b/server/objects/DailyLog.js
index 1dc02bed..17fd03c8 100644
--- a/server/objects/DailyLog.js
+++ b/server/objects/DailyLog.js
@@ -60,15 +60,14 @@ class DailyLog {
var oneBigLog = ''
buffered.forEach((logLine) => {
- oneBigLog += logLine + '\n'
+ oneBigLog += logLine
})
-
this.appendLogLine(oneBigLog)
}
async appendLog(logObj) {
this.logs.push(logObj)
- var line = JSON.stringify(logObj)
+ var line = JSON.stringify(logObj) + '\n'
this.appendLogLine(line)
}
@@ -97,16 +96,31 @@ class DailyLog {
}
var text = await readTextFile(this.fullPath)
+
+ var hasFailures = false
+
this.logs = text.split(/\r?\n/).map(t => {
+ if (!t) {
+ hasFailures = true
+ return null
+ }
try {
return JSON.parse(t)
} catch (err) {
console.error('Failed to parse log line', t, err)
+ hasFailures = true
return null
}
}).filter(l => !!l)
- Logger.info(`[DailyLog] ${this.id}: Loaded ${this.logs.length} Logs`)
+ // Rewrite log file to remove errors
+ if (hasFailures) {
+ var newLogLines = this.logs.map(l => JSON.stringify(l)).join('\n') + '\n'
+ await fs.writeFile(this.fullPath, newLogLines)
+ console.log('Re-Saved log file to remove bad lines')
+ }
+
+ Logger.debug(`[DailyLog] ${this.id}: Loaded ${this.logs.length} Logs`)
}
}
module.exports = DailyLog
\ No newline at end of file