40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { dirname, join } from "path";
|
|
import { mkdir, open, readFile } from "fs/promises"
|
|
|
|
export namespace OutputHandler {
|
|
export const LogCachePath = join(
|
|
process.cwd(),
|
|
"cache",
|
|
"log",
|
|
);
|
|
export const ErrorLogPath = join(LogCachePath, "error.log" );
|
|
export function getErrorOutput(...args: any[]) {
|
|
const timestamp = new Date().toISOString();
|
|
|
|
const message = args
|
|
.map(arg => {
|
|
if (arg instanceof Error) {
|
|
return `${arg.name}: ${arg.message}\n${arg.stack}`;
|
|
}
|
|
if (typeof arg === 'object') {
|
|
return JSON.stringify(arg);
|
|
}
|
|
return String(arg);
|
|
})
|
|
.join(' ');
|
|
|
|
|
|
return `[${timestamp}] ${message}`;
|
|
}
|
|
export async function errorLog(...args: any[]): Promise<void> {
|
|
const output = getErrorOutput(...args);
|
|
await mkdir(dirname(ErrorLogPath), { recursive: true });
|
|
|
|
const fileHandle = await open(ErrorLogPath, "a");
|
|
fileHandle.write(output + "\n");
|
|
fileHandle.close();
|
|
}
|
|
export async function getErrorLog(): Promise<string> {
|
|
return (await readFile(ErrorLogPath)).toString();
|
|
}
|
|
}
|