From 2585b73723be8a22d3b10950eb41849793c96fde Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 10 Jul 2023 16:52:26 +0200 Subject: [PATCH] Use native history --- packages/backend-core/src/logging/system.ts | 35 ++++++++++++++------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/backend-core/src/logging/system.ts b/packages/backend-core/src/logging/system.ts index 98f2b155a5..d5d19018c7 100644 --- a/packages/backend-core/src/logging/system.ts +++ b/packages/backend-core/src/logging/system.ts @@ -5,28 +5,41 @@ import * as rfs from "rotating-file-stream" import env from "../environment" import { budibaseTempDir } from "../objectStore" -const logsFileName = path.join(budibaseTempDir(), `budibase.logs`) -const rollingFileName = `${logsFileName}.bak` +const logsFileName = `budibase.logs` +const budibaseLogsHistoryFileName = "budibase-logs-history.txt" + +const logsPath = path.join(budibaseTempDir(), "systemlogs") + +function getFullPath(fileName: string) { + return path.join(logsPath, fileName) +} export function localFileDestination() { const outFile = rfs.createStream(logsFileName, { size: env.ROLLING_LOG_MAX_SIZE, - }) - - outFile.on("rotation", () => { - fs.copyFileSync(logsFileName, rollingFileName) + path: logsPath, + maxFiles: 1, + immutable: true, + history: "budibase-logs-history.txt", + initialRotation: false, }) return outFile } export function getLogReadStream() { - const logsContent = fs.readFileSync(logsFileName) - if (!fs.existsSync(rollingFileName)) { - return logsContent + const streams = [] + const historyFile = getFullPath(budibaseLogsHistoryFileName) + if (fs.existsSync(historyFile)) { + const fileContent = fs.readFileSync(historyFile, "utf-8") + const historyFiles = fileContent.split("\n") + for (const historyFile of historyFiles.filter(x => x)) { + streams.push(fs.readFileSync(historyFile)) + } } - const rollingContent = fs.readFileSync(rollingFileName) - const combinedContent = Buffer.concat([rollingContent, logsContent]) + streams.push(fs.readFileSync(getFullPath(logsFileName))) + + const combinedContent = Buffer.concat(streams) return combinedContent }