-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
46 lines (41 loc) · 1.36 KB
/
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
38
39
40
41
42
43
44
45
46
const winston = require('winston');
require('winston-daily-rotate-file');
// Define log format
const logFormat = winston.format.printf(({ timestamp, level, message, meta }) => {
return `${timestamp} [${level.toUpperCase()}]: ${message} ${meta ? JSON.stringify(meta) : ''}`;
});
// Create a logger with daily rotation for different log levels
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
logFormat
),
transports: [
// Error logs
new winston.transports.DailyRotateFile({
filename: 'logs/errors-%DATE%.log',
datePattern: 'YYYY-MM-DD',
level: 'error',
maxFiles: '30d'
}),
// Info logs
new winston.transports.DailyRotateFile({
filename: 'logs/combined-%DATE%.log',
datePattern: 'YYYY-MM-DD',
level: 'info',
maxFiles: '30d'
}),
// For console logs during development
new winston.transports.Console({
format: winston.format.simple(),
})
]
});
// Helper functions for logging at different levels
const logError = (message, meta) => {
logger.error(message, { meta });
};
const logInfo = (message, meta) => {
logger.info(message, { meta });
};
module.exports = { logger, logError, logInfo };