budibase/packages/backend-core/src/logging/system.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

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-11 13:09:51 +02:00
const logsFileName = `budibase.log`
2023-07-10 16:52:26 +02:00
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,
2023-07-10 17:17:45 +02:00
history: budibaseLogsHistoryFileName,
2023-07-10 16:52:26 +02:00
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
}