Files
anything-llm/collector/middleware/httpLogger.js
Marcello Fitton eb77876127 Add HTTP request/response logging middleware for development mode (#4425)
* Add HTTP request logging middleware for development mode

- Introduced httpLogger middleware to log HTTP requests and responses.
- Enabled logging only in development mode to assist with debugging.

* Update httpLogger middleware to disable time logging by default

* Add httpLogger middleware for development mode in collector service

* Refactor httpLogger middleware to rename timeLogs parameter to enableTimestamps for clarity

* Make HTTP Logger only mount in development and environment flag is enabled.

* Update .env.example to clarify HTTP Logger configuration comments

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2025-09-29 13:33:15 -07:00

30 lines
827 B
JavaScript

const httpLogger =
({ enableTimestamps = false }) =>
(req, res, next) => {
// Capture the original res.end to log response status
const originalEnd = res.end;
res.end = function (chunk, encoding) {
// Log the request method, status code, and path
const statusColor = res.statusCode >= 400 ? "\x1b[31m" : "\x1b[32m"; // Red for errors, green for success
console.log(
`\x1b[32m[HTTP]\x1b[0m ${statusColor}${res.statusCode}\x1b[0m ${
req.method
} -> ${req.path} ${
enableTimestamps
? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}`
: ""
}`.trim()
);
// Call the original end method
return originalEnd.call(this, chunk, encoding);
};
next();
};
module.exports = {
httpLogger,
};