-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
37 lines (33 loc) · 880 Bytes
/
logger.js
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
const winston = require("winston");
const fs = require("fs");
const path = require("path");
const logDir = "logs";
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const logger = winston.createLogger({
level: "info",
format: winston.format.json(),
defaultMeta: { service: "user-service" },
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: path.join(logDir, "error.log"),
level: "error",
}),
new winston.transports.File({
filename: path.join(logDir, "combined.log"),
}),
],
});
setInterval(() => {
try {
// Clear error.log file
fs.writeFileSync("./logs/error.log", "");
// Clear combined.log file
fs.writeFileSync("./logs/combined.log", "");
} catch (err) {
logger.error(`Error clearing log files: ${err.message}`);
}
}, 86400000);
module.exports = logger;