2023-07-10 13:25:24 +02:00
|
|
|
import fs from "fs"
|
|
|
|
import path from "path"
|
|
|
|
import * as rfs from "rotating-file-stream"
|
|
|
|
|
|
|
|
import env from "../environment"
|
|
|
|
import { budibaseTempDir } from "../objectStore"
|
|
|
|
|
2023-07-10 16:52:26 +02:00
|
|
|
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)
|
|
|
|
}
|
2023-07-10 13:25:24 +02:00
|
|
|
|
|
|
|
export function localFileDestination() {
|
|
|
|
const outFile = rfs.createStream(logsFileName, {
|
|
|
|
size: env.ROLLING_LOG_MAX_SIZE,
|
2023-07-10 16:52:26 +02:00
|
|
|
path: logsPath,
|
|
|
|
maxFiles: 1,
|
|
|
|
immutable: true,
|
|
|
|
history: "budibase-logs-history.txt",
|
|
|
|
initialRotation: false,
|
2023-07-10 13:25:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return outFile
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLogReadStream() {
|
2023-07-10 16:52:26 +02:00
|
|
|
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))
|
|
|
|
}
|
2023-07-10 13:25:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-10 16:52:26 +02:00
|
|
|
streams.push(fs.readFileSync(getFullPath(logsFileName)))
|
|
|
|
|
|
|
|
const combinedContent = Buffer.concat(streams)
|
2023-07-10 13:25:24 +02:00
|
|
|
return combinedContent
|
|
|
|
}
|