-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.ts
More file actions
74 lines (65 loc) · 2.01 KB
/
logger.ts
File metadata and controls
74 lines (65 loc) · 2.01 KB
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
74
import winston from "winston";
import dotenv from "dotenv";
import DailyRotateFile from "winston-daily-rotate-file";
dotenv.config();
// RFC5424 syslog levels (Winston's syslog mapping)
enum LOG_LEVEL {
emerg = "emerg",
alert = "alert",
crit = "crit",
error = "error",
warning = "warning",
notice = "notice",
info = "info",
debug = "debug"
}
// Automatically detect package name from working directory
function detectPackageName(): string {
// Example: ....\ms3-package\packages\wallet\...
const cwd = process.cwd();
const match = cwd.match(/ms3-package[\\/](?:packages[\\/])([^\\/]+)/);
return match ? match[1] : "shared";
}
const rawLevel = process.env.LOG_LEVEL ?? 'info';
const level = typeof rawLevel === 'string' ? rawLevel.toUpperCase() : String(rawLevel).toUpperCase();
const logDir = process.env.LOG_DIR || "logs";
const packageName = detectPackageName();
const transports: winston.transport[] = [
new winston.transports.Console({
level,
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
),
}),
new DailyRotateFile({
filename: `${logDir}/${packageName}-%DATE%.log`,
datePattern: "YYYY-MM-DD",
level,
maxSize: "10m",
maxFiles: "14d",
format: winston.format.json(),
}),
];
class LoggerSingleton {
private static _instance: winston.Logger;
static get instance(): winston.Logger {
if (!LoggerSingleton._instance) {
LoggerSingleton._instance = winston.createLogger({
level,
levels: winston.config.syslog.levels,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ timestamp, level, message, ...meta }) => {
const context = meta.package || meta.script || "";
return `[${timestamp}]${context ? " [" + context + "]" : ""} ${level}: ${message}`;
})
),
transports,
});
}
return LoggerSingleton._instance;
}
}
const logger = LoggerSingleton.instance;
export { logger, LOG_LEVEL };