fix(#12): Implement structured logging for API errors and security events
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit is contained in:
parent
f860f47018
commit
32480438c7
2 changed files with 26 additions and 0 deletions
25
backend/src/middleware/logger.js
Normal file
25
backend/src/middleware/logger.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function logger(req, res, next) {
|
||||
req.requestId = Math.random().toString(36).substring(2, 10);
|
||||
console.log(JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
requestId: req.requestId,
|
||||
level: 'info'
|
||||
}));
|
||||
res.on('finish', () => {
|
||||
const securityEvent = [401, 403].includes(res.statusCode);
|
||||
console.log(JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
status: res.statusCode,
|
||||
requestId: req.requestId,
|
||||
level: res.statusCode >= 400 ? 'error' : 'info',
|
||||
securityEvent: securityEvent
|
||||
}));
|
||||
});
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = logger;
|
||||
|
|
@ -7,6 +7,7 @@ import reviewRoutes from './routes/reviews.js';
|
|||
import addressRoutes from './routes/addresses.js';
|
||||
import contactRoutes from './routes/contacts.js';
|
||||
import profileRoutes from './routes/profile.js';
|
||||
import logger from './middleware/logger.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue