Files
lynq-chat/packages/backend/src/lib/logger.ts
T

34 lines
985 B
TypeScript
Executable File

import { appendFileSync } from "node:fs";
import { EOL } from "node:os";
const createLog = (
nativeLogger: (...args: any[]) => void,
type: string,
...args: any[]
) => {
if (args[0] instanceof Array) {
args = args[0];
}
const content = `${new Date().toLocaleString()} [${type}] ${args.join("\n").replaceAll("\n", "\n ")}`;
nativeLogger(content);
try {
appendFileSync(
`${import.meta.dirname}/../../../../.log/system.log`,
`${content.toString().replace(/\x1B\[[0-9;]*m/g, '')}${EOL}`
);
} catch (err) {
console.error(`${new Date().toLocaleString()} Logger: Failed to write to file.`);
}
}
const logger = {
log: (...args: any[]) => createLog(console.log, "LOG", args),
info: (...args: any[]) => createLog(console.info, "INFO", args),
error: (...args: any[]) => createLog(console.error, "ERROR", args),
warn: (...args: any[]) => createLog(console.warn, "WARN", args),
}
export default logger;