Skip to content

Commit eb77876

Browse files
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 <[email protected]>
1 parent 8fc1f24 commit eb77876

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

collector/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# Placeholder .env file for collector runtime
2+
3+
# This enables HTTP request/response logging in development. Set value to truthy string to enable, leave empty value or comment out to disable
4+
# ENABLE_HTTP_LOGGER=""
5+
# This enables timestamps for the HTTP Logger. Set value to true to enable, leave empty or comment out to disable
6+
# ENABLE_HTTP_LOGGER_TIMESTAMPS=""

collector/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ yarn-error.log
44
!yarn.lock
55
outputs
66
scripts
7+
.env.development
8+
.env.production
9+
.env.test

collector/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ const { wipeCollectorStorage } = require("./utils/files");
1515
const extensions = require("./extensions");
1616
const { processRawText } = require("./processRawText");
1717
const { verifyPayloadIntegrity } = require("./middleware/verifyIntegrity");
18+
const { httpLogger } = require("./middleware/httpLogger");
1819
const app = express();
1920
const FILE_LIMIT = "3GB";
2021

22+
// Only log HTTP requests in development mode and if the ENABLE_HTTP_LOGGER environment variable is set to true
23+
if (
24+
process.env.NODE_ENV === "development" &&
25+
!!process.env.ENABLE_HTTP_LOGGER
26+
) {
27+
app.use(
28+
httpLogger({
29+
enableTimestamps: !!process.env.ENABLE_HTTP_LOGGER_TIMESTAMPS,
30+
})
31+
);
32+
}
2133
app.use(cors({ origin: true }));
2234
app.use(
2335
bodyParser.text({ limit: FILE_LIMIT }),

collector/middleware/httpLogger.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const httpLogger =
2+
({ enableTimestamps = false }) =>
3+
(req, res, next) => {
4+
// Capture the original res.end to log response status
5+
const originalEnd = res.end;
6+
7+
res.end = function (chunk, encoding) {
8+
// Log the request method, status code, and path
9+
const statusColor = res.statusCode >= 400 ? "\x1b[31m" : "\x1b[32m"; // Red for errors, green for success
10+
console.log(
11+
`\x1b[32m[HTTP]\x1b[0m ${statusColor}${res.statusCode}\x1b[0m ${
12+
req.method
13+
} -> ${req.path} ${
14+
enableTimestamps
15+
? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}`
16+
: ""
17+
}`.trim()
18+
);
19+
20+
// Call the original end method
21+
return originalEnd.call(this, chunk, encoding);
22+
};
23+
24+
next();
25+
};
26+
27+
module.exports = {
28+
httpLogger,
29+
};

server/.env.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,9 @@ TTS_PROVIDER="native"
368368
# Runtime flags for built-in pupeeteer Chromium instance
369369
# This is only required on Linux machines running AnythingLLM via Docker
370370
# and do not want to use the --cap-add=SYS_ADMIN docker argument
371-
# ANYTHINGLLM_CHROMIUM_ARGS="--no-sandbox,--disable-setuid-sandbox"
371+
# ANYTHINGLLM_CHROMIUM_ARGS="--no-sandbox,--disable-setuid-sandbox"
372+
373+
# This enables HTTP request/response logging in development. Set value to a truthy string to enable, leave empty value or comment out to disable.
374+
# ENABLE_HTTP_LOGGER=""
375+
# This enables timestamps for the HTTP Logger. Set value to a truthy string to enable, leave empty value or comment out to disable.
376+
# ENABLE_HTTP_LOGGER_TIMESTAMPS=""

server/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,22 @@ const { communityHubEndpoints } = require("./endpoints/communityHub");
2929
const { agentFlowEndpoints } = require("./endpoints/agentFlows");
3030
const { mcpServersEndpoints } = require("./endpoints/mcpServers");
3131
const { mobileEndpoints } = require("./endpoints/mobile");
32+
const { httpLogger } = require("./middleware/httpLogger");
3233
const app = express();
3334
const apiRouter = express.Router();
3435
const FILE_LIMIT = "3GB";
3536

37+
// Only log HTTP requests in development mode and if the ENABLE_HTTP_LOGGER environment variable is set to true
38+
if (
39+
process.env.NODE_ENV === "development" &&
40+
!!process.env.ENABLE_HTTP_LOGGER
41+
) {
42+
app.use(
43+
httpLogger({
44+
enableTimestamps: !!process.env.ENABLE_HTTP_LOGGER_TIMESTAMPS,
45+
})
46+
);
47+
}
3648
app.use(cors({ origin: true }));
3749
app.use(bodyParser.text({ limit: FILE_LIMIT }));
3850
app.use(bodyParser.json({ limit: FILE_LIMIT }));

server/middleware/httpLogger.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const httpLogger =
2+
({ enableTimestamps = false }) =>
3+
(req, res, next) => {
4+
// Capture the original res.end to log response status
5+
const originalEnd = res.end;
6+
7+
res.end = function (chunk, encoding) {
8+
// Log the request method, status code, and path
9+
const statusColor = res.statusCode >= 400 ? "\x1b[31m" : "\x1b[32m"; // Red for errors, green for success
10+
console.log(
11+
`\x1b[32m[HTTP]\x1b[0m ${statusColor}${res.statusCode}\x1b[0m ${req.method} -> ${req.path} ${enableTimestamps ? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}` : ""}`.trim()
12+
);
13+
14+
// Call the original end method
15+
return originalEnd.call(this, chunk, encoding);
16+
};
17+
18+
next();
19+
};
20+
21+
module.exports = {
22+
httpLogger,
23+
};

0 commit comments

Comments
 (0)