forked from fosrl/pangolin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.ts
73 lines (66 loc) · 2.16 KB
/
logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import "winston-daily-rotate-file";
import config from "@server/lib/config";
import * as winston from "winston";
import path from "path";
import { APP_PATH } from "./lib/consts";
const hformat = winston.format.printf(
({ level, label, message, timestamp, stack, ...metadata }) => {
let msg = `${timestamp} [${level}]${label ? `[${label}]` : ""}: ${message}`;
if (stack) {
msg += `\nStack: ${stack}`;
}
if (Object.keys(metadata).length > 0) {
msg += ` ${JSON.stringify(metadata)}`;
}
return msg;
}
);
const transports: any = [new winston.transports.Console({})];
if (config.getRawConfig().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.getRawConfig().app.log_level.toLowerCase(),
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.colorize(),
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;