-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.js
More file actions
129 lines (114 loc) · 3.15 KB
/
Copy pathlogger.js
File metadata and controls
129 lines (114 loc) · 3.15 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
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
/**
* Winston Logger Configuration
* Replaces console.log with structured logging
*/
const winston = require('winston');
const path = require('path');
// Define log format
const logFormat = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.json()
);
// Console format for development
const consoleFormat = winston.format.combine(
winston.format.colorize(),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.printf(({ timestamp, level, message, ...meta }) => {
let msg = `${timestamp} [${level}]: ${message}`;
if (Object.keys(meta).length > 0) {
msg += ` ${JSON.stringify(meta)}`;
}
return msg;
})
);
// Create logger instance
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || (process.env.NODE_ENV === 'production' ? 'info' : 'debug'),
format: logFormat,
defaultMeta: { service: 'rsastore' },
transports: [
// Error logs - separate file
new winston.transports.File({
filename: path.join(__dirname, 'logs', 'error.log'),
level: 'error',
maxsize: 10485760, // 10MB
maxFiles: 5
}),
// Combined logs - all levels
new winston.transports.File({
filename: path.join(__dirname, 'logs', 'combined.log'),
maxsize: 10485760, // 10MB
maxFiles: 10
})
]
});
// Add console transport in development
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: consoleFormat
}));
} else {
// In production, only log warnings and errors to console
logger.add(new winston.transports.Console({
format: consoleFormat,
level: 'warn'
}));
}
// Create a stream object for Morgan HTTP logging
logger.stream = {
write: (message) => {
logger.info(message.trim());
}
};
// Helper methods for common log patterns
logger.logRequest = (req, meta = {}) => {
logger.info('HTTP Request', {
method: req.method,
url: req.url,
ip: req.ip,
userAgent: req.get('user-agent'),
...meta
});
};
logger.logError = (error, req = null, meta = {}) => {
const errorLog = {
message: error.message,
stack: error.stack,
...meta
};
if (req) {
errorLog.request = {
method: req.method,
url: req.url,
ip: req.ip
};
}
logger.error('Error occurred', errorLog);
};
logger.logAuth = (action, userId, success, meta = {}) => {
logger.info('Authentication', {
action,
userId,
success,
...meta
});
};
logger.logDatabase = (action, table, meta = {}) => {
logger.debug('Database operation', {
action,
table,
...meta
});
};
logger.logPayment = (action, invoiceNumber, amount, status, meta = {}) => {
logger.info('Payment', {
action,
invoiceNumber,
amount,
status,
...meta
});
};
module.exports = logger;