fosrl.pangolin/server/logger.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-09-27 21:39:03 -04:00
import "winston-daily-rotate-file";
2024-10-12 18:21:31 -04:00
import config, { APP_PATH } from "@server/config";
2024-09-27 21:39:03 -04:00
import * as winston from "winston";
import path from "path";
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
2024-10-12 18:21:31 -04:00
if (config.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
);
}
const logger = winston.createLogger({
2024-10-12 18:21:31 -04:00
level: config.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;