-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
137 lines (116 loc) · 3.95 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const winston = require('winston');
require('winston-daily-rotate-file');
const path = require('path');
const fs = require('fs');
const cron = require('node-cron');
const { createLogger, format, transports } = winston;
const { combine, timestamp, printf, errors } = format;
// Ensure log directories exist
const logTypes = ['access', 'detail', 'level'];
logTypes.forEach(type => {
const dir = path.join(__dirname, '..', 'logs', type);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
});
// Custom format for date
const customTimestamp = format((info) => {
info.timestamp = new Date().toLocaleString('en-GB', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
return info;
});
// Define custom log format for access logs
const accessLogFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level.toUpperCase()}: ${message}`;
});
// Define custom log format for detailed logs
const detailLogFormat = printf(({ level, message, timestamp, ...metadata }) => {
let logMessage = `${timestamp} ${level.toUpperCase()}: ${message}`;
if (Object.keys(metadata).length) {
logMessage += ` | ${JSON.stringify(metadata)}`;
}
return logMessage;
});
// Create transports for each log type
const createTransport = (type, format) => {
return new transports.DailyRotateFile({
filename: path.join(__dirname, '..', 'logs', type, `%DATE% ${type}.log`),
datePattern: 'DD-MM-YYYY',
zippedArchive: true,
maxSize: '200m',
maxFiles: '14d',
format: combine(
customTimestamp(),
format
)
});
};
// Create logger
const logger = createLogger({
transports: [
createTransport('access', accessLogFormat),
createTransport('detail', combine(errors({ stack: true }), detailLogFormat)),
createTransport('level', printf(({ level, message, timestamp }) => {
return `${timestamp} ${level.toUpperCase()}: ${message}`;
}))
]
});
// Middleware to log request and response details
logger.requestLogger = (req, res, next) => {
const { method, url, ip } = req;
logger.info(`${method} ${url}`, { ip });
const start = Date.now();
const originalSend = res.send;
res.send = function (data) {
const duration = Date.now() - start;
const status = res.statusCode;
const statusMessage = res.statusMessage;
logger.info('Outgoing Response', {
method,
url,
status,
statusMessage,
duration: `${duration}ms`,
responseData: data,
responseHeaders: res.getHeaders()
});
return originalSend.call(this, data);
};
next();
};
// Set up cron job to rotate logs daily
cron.schedule('0 0 * * *', () => {
logger.info('Rotating logs for a new day');
logTypes.forEach(type => {
const transport = logger.transports.find(t => t.name === 'dailyRotateFile' && t.dirname.includes(type));
if (transport) {
transport.rotate();
}
});
});
module.exports = logger;
// Creates a new file daily with the format "DD-MM-YYYY type.log"
// Limits each file to 200MB
// Keeps logs for 14 days
// Automatically creates new parts (e.g., "28-08-2024 detail-1.log") if a file exceeds 200MB
// 3 level honge,
//1 level sent to user that we will sent to user , normal file (name: access.log), all log single line statement
//2 level , 2 level sent to all logs but in detail (name: detail.log)
// log rotation of both the L1, L3
// 3 level extra (error.log)
// Numerical Severity
// Code
// 0 Emergency: system is unusable
// 1 Alert: action must be taken immediately
// 2 Critical: critical conditions
// 3 Error: error conditions
// 4 Warning: warning conditions
// 5 Notice: normal but significant condition
// 6 Informational: informational messages
// 7 Debug: debug-level messages