fosrl.pangolin/server/logger.ts

74 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2024-09-27 21:39:03 -04:00
import "winston-daily-rotate-file";
2025-01-01 21:41:31 -05:00
import config from "@server/lib/config";
2024-09-27 21:39:03 -04:00
import * as winston from "winston";
import path from "path";
2025-01-01 21:41:31 -05:00
import { APP_PATH } from "./lib/consts";
2024-09-27 21:39:03 -04:00
const hformat = winston.format.printf(
2024-12-20 22:24:44 -05:00
({ level, label, message, timestamp, stack, ...metadata }) => {
let msg = `${timestamp} [${level}]${label ? `[${label}]` : ""}: ${message}`;
if (stack) {
msg += `\nStack: ${stack}`;
}
2024-09-27 21:39:03 -04:00
if (Object.keys(metadata).length > 0) {
2024-12-20 22:24:44 -05:00
msg += ` ${JSON.stringify(metadata)}`;
2024-09-27 21:39:03 -04:00
}
return msg;
2024-12-20 22:24:44 -05:00
}
2024-09-27 21:39:03 -04:00
);
2024-12-20 22:24:44 -05:00
const transports: any = [new winston.transports.Console({})];
2024-09-27 21:39:03 -04:00
if (config.getRawConfig().app.save_logs) {
2024-09-27 21:39:03 -04:00
transports.push(
new winston.transports.DailyRotateFile({
2024-12-20 22:24:44 -05:00
filename: path.join(APP_PATH, "logs", "pangolin-%DATE%.log"),
2024-09-27 21:39:03 -04:00
datePattern: "YYYY-MM-DD",
zippedArchive: true,
maxSize: "20m",
maxFiles: "7d",
createSymlink: true,
2024-12-20 22:24:44 -05:00
symlinkName: "pangolin.log"
})
2024-09-27 21:39:03 -04:00
);
2024-12-20 22:33:22 -05:00
transports.push(
new winston.transports.DailyRotateFile({
filename: path.join(APP_PATH, "logs", ".machinelogs-%DATE%.json"),
datePattern: "YYYY-MM-DD",
zippedArchive: true,
maxSize: "20m",
maxFiles: "1d",
createSymlink: true,
symlinkName: ".machinelogs.json",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.splat(),
winston.format.json()
)
})
);
2024-09-27 21:39:03 -04:00
}
const logger = winston.createLogger({
level: config.getRawConfig().app.log_level.toLowerCase(),
2024-09-27 21:39:03 -04:00
format: winston.format.combine(
2024-12-20 22:24:44 -05:00
winston.format.errors({ stack: true }),
winston.format.colorize(),
2024-09-27 21:39:03 -04:00
winston.format.splat(),
winston.format.timestamp(),
2024-12-20 22:24:44 -05:00
hformat
2024-09-27 21:39:03 -04:00
),
2024-12-20 22:24:44 -05:00
transports
2024-09-27 21:39:03 -04:00
});
2024-12-20 22:24:44 -05:00
process.on("uncaughtException", (error) => {
logger.error("Uncaught Exception:", { error, stack: error.stack });
process.exit(1);
});
process.on("unhandledRejection", (reason, _) => {
logger.error("Unhandled Rejection:", { reason });
});
2024-09-28 14:07:25 -04:00
2024-09-27 21:39:03 -04:00
export default logger;