Name of the log file to read
Maximum number of lines to return
Array of log lines
export function getLogsFromFile(
fileName: string,
count: number = 100,
): string[] {
try {
const filePath = path.join(logsPath, fileName);
if (!fs.existsSync(filePath)) {
return [
`[${new Date().toLocaleTimeString()}] [ERROR] Log file not found: ${fileName}`,
];
}
// Read only the specified file, never mixing with other log files
const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n").filter((line) => line.trim() !== "");
// Return at most 'count' lines from just this file
return lines.slice(-count);
} catch (error) {
console.error(`Error reading log file ${fileName}:`, error);
return [
`[${new Date().toLocaleTimeString()}] [ERROR] Error reading log file: ${error}`,
];
}
}
Reads logs from a specific file