-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
67 lines (59 loc) · 1.84 KB
/
index.d.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
import winston from "winston";
import 'winston-daily-rotate-file';
class Logger {
// Private properties
#transporters = [];
#myFormat = winston.format.printf(({ level, message, label, timestamp }) => {
return `${timestamp} : ${level} : ${message}`;
});
#logDir = 'logs';
// Setter for log directory
setLogDir(dir) {
this.#logDir = dir;
}
// Setter for log format
setFormat(format) {
this.#myFormat = winston.format.printf(format);
}
// Method to add custom transporters
transporter(transporters) {
for (const transporter of transporters) {
// If the transporter doesn't have a format, assign the default format
if (!transporter.format) {
transporter.format = this.#format();
}
}
// Concatenate the new transporters with existing ones
this.#transporters = [...this.#transporters, ...transporters];
}
// Method to get or create a logger
getLogger(label) {
if (!winston.loggers.has(label)) {
// Add a file transport specific to the provided label
let logTransporter = [...this.#transporters,this.#fileTransport(label)];
// Create or get the logger with specified transports and label format
winston.loggers.add(label, {
transports: logTransporter,
format: winston.format.label({ label }),
});
}
// Return the logger with the specified label
return winston.loggers.get(label);
}
// Private method to define the log format
#format() {
return winston.format.combine(
winston.format.timestamp(),
this.#myFormat,
);
}
// Private method to configure the file transport
#fileTransport = (label) => {
return new winston.transports.DailyRotateFile({
format: this.#format(),
filename: `${this.#logDir}/${label}_%DATE%.log`,
datePattern: 'YYYY_MM_DD',
});
};
}
export default Logger;