mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-13 05:40:38 +01:00
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import "winston-daily-rotate-file";
|
|
import config, { APP_PATH } from "@server/config";
|
|
import * as winston from "winston";
|
|
import path from "path";
|
|
|
|
const hformat = winston.format.printf(
|
|
({ level, label, message, timestamp, ...metadata }) => {
|
|
let msg = `${timestamp} [${level}]${label ? `[${label}]` : ""}: ${message} `;
|
|
if (Object.keys(metadata).length > 0) {
|
|
msg += JSON.stringify(metadata);
|
|
}
|
|
return msg;
|
|
},
|
|
);
|
|
|
|
const transports: any = [
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.splat(),
|
|
winston.format.timestamp(),
|
|
hformat,
|
|
),
|
|
}),
|
|
];
|
|
|
|
if (config.app.save_logs) {
|
|
transports.push(
|
|
new winston.transports.DailyRotateFile({
|
|
filename: path.join(
|
|
APP_PATH,
|
|
"logs",
|
|
"pangolin-%DATE%.log",
|
|
),
|
|
datePattern: "YYYY-MM-DD",
|
|
zippedArchive: true,
|
|
maxSize: "20m",
|
|
maxFiles: "7d",
|
|
createSymlink: true,
|
|
symlinkName: "pangolin.log",
|
|
}),
|
|
);
|
|
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(),
|
|
),
|
|
}),
|
|
);
|
|
}
|
|
|
|
const logger = winston.createLogger({
|
|
level: config.app.log_level.toLowerCase(),
|
|
format: winston.format.combine(
|
|
winston.format.splat(),
|
|
winston.format.timestamp(),
|
|
hformat,
|
|
),
|
|
transports,
|
|
});
|
|
|
|
// process.on("uncaughtException", (error) => {
|
|
// logger.error("Uncaught Exception:", { error, stack: error.stack });
|
|
// process.exit(1);
|
|
// });
|
|
//
|
|
// process.on("unhandledRejection", (reason, _) => {
|
|
// logger.error("Unhandled Rejection:", { reason });
|
|
// });
|
|
|
|
export default logger;
|