From f510ed558e78569fa3a1842e3afd5e9e4ab22f9e Mon Sep 17 00:00:00 2001 From: angelplusultra Date: Mon, 22 Sep 2025 14:18:56 -0700 Subject: [PATCH 1/6] 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. --- server/index.js | 5 +++++ server/middleware/httpLogger.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 server/middleware/httpLogger.js diff --git a/server/index.js b/server/index.js index 1779035d552..29e6625f8a7 100644 --- a/server/index.js +++ b/server/index.js @@ -29,10 +29,15 @@ const { communityHubEndpoints } = require("./endpoints/communityHub"); const { agentFlowEndpoints } = require("./endpoints/agentFlows"); const { mcpServersEndpoints } = require("./endpoints/mcpServers"); const { mobileEndpoints } = require("./endpoints/mobile"); +const { httpLogger } = require("./middleware/httpLogger"); const app = express(); const apiRouter = express.Router(); const FILE_LIMIT = "3GB"; +// Only log HTTP requests in development mode +if (process.env.NODE_ENV === "development") { + app.use(httpLogger({ timeLogs: false })); +} app.use(cors({ origin: true })); app.use(bodyParser.text({ limit: FILE_LIMIT })); app.use(bodyParser.json({ limit: FILE_LIMIT })); diff --git a/server/middleware/httpLogger.js b/server/middleware/httpLogger.js new file mode 100644 index 00000000000..6b9d7688dc1 --- /dev/null +++ b/server/middleware/httpLogger.js @@ -0,0 +1,23 @@ +const httpLogger = + ({ timeLogs = true }) => + (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} ${timeLogs ? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}` : ""}`.trim() + ); + + // Call the original end method + return originalEnd.call(this, chunk, encoding); + }; + + next(); + }; + +module.exports = { + httpLogger, +}; From de94fad7aaa226e52249d329b46aecdc1ba7d9a8 Mon Sep 17 00:00:00 2001 From: angelplusultra Date: Mon, 22 Sep 2025 14:29:36 -0700 Subject: [PATCH 2/6] Update httpLogger middleware to disable time logging by default --- server/middleware/httpLogger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/middleware/httpLogger.js b/server/middleware/httpLogger.js index 6b9d7688dc1..c1ccf181e86 100644 --- a/server/middleware/httpLogger.js +++ b/server/middleware/httpLogger.js @@ -1,5 +1,5 @@ const httpLogger = - ({ timeLogs = true }) => + ({ timeLogs = false }) => (req, res, next) => { // Capture the original res.end to log response status const originalEnd = res.end; From 8e2f1bfb08c8a8095a234dc6c854cd3c32103207 Mon Sep 17 00:00:00 2001 From: angelplusultra Date: Mon, 22 Sep 2025 14:56:30 -0700 Subject: [PATCH 3/6] Add httpLogger middleware for development mode in collector service --- collector/index.js | 4 ++++ collector/middleware/httpLogger.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 collector/middleware/httpLogger.js diff --git a/collector/index.js b/collector/index.js index a0dd0e5644d..e0b53300f2e 100644 --- a/collector/index.js +++ b/collector/index.js @@ -15,9 +15,13 @@ const { wipeCollectorStorage } = require("./utils/files"); const extensions = require("./extensions"); const { processRawText } = require("./processRawText"); const { verifyPayloadIntegrity } = require("./middleware/verifyIntegrity"); +const { httpLogger } = require("./middleware/httpLogger"); const app = express(); const FILE_LIMIT = "3GB"; +if (process.env.NODE_ENV === "development") { + app.use(httpLogger({ timeLogs: false })); +} app.use(cors({ origin: true })); app.use( bodyParser.text({ limit: FILE_LIMIT }), diff --git a/collector/middleware/httpLogger.js b/collector/middleware/httpLogger.js new file mode 100644 index 00000000000..dcdeb95c17a --- /dev/null +++ b/collector/middleware/httpLogger.js @@ -0,0 +1,29 @@ +const httpLogger = + ({ timeLogs = 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} ${ + timeLogs + ? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}` + : "" + }`.trim() + ); + + // Call the original end method + return originalEnd.call(this, chunk, encoding); + }; + + next(); + }; + +module.exports = { + httpLogger, +}; From 8fdfcbe27efdf3211e97cb0338b30a23322aecb8 Mon Sep 17 00:00:00 2001 From: angelplusultra Date: Fri, 26 Sep 2025 09:26:20 -0700 Subject: [PATCH 4/6] Refactor httpLogger middleware to rename timeLogs parameter to enableTimestamps for clarity --- collector/middleware/httpLogger.js | 4 ++-- server/middleware/httpLogger.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/collector/middleware/httpLogger.js b/collector/middleware/httpLogger.js index dcdeb95c17a..6fa20be041d 100644 --- a/collector/middleware/httpLogger.js +++ b/collector/middleware/httpLogger.js @@ -1,5 +1,5 @@ const httpLogger = - ({ timeLogs = false }) => + ({ enableTimestamps = false }) => (req, res, next) => { // Capture the original res.end to log response status const originalEnd = res.end; @@ -11,7 +11,7 @@ const httpLogger = `\x1b[32m[HTTP]\x1b[0m ${statusColor}${res.statusCode}\x1b[0m ${ req.method } -> ${req.path} ${ - timeLogs + enableTimestamps ? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}` : "" }`.trim() diff --git a/server/middleware/httpLogger.js b/server/middleware/httpLogger.js index c1ccf181e86..627505ffdd8 100644 --- a/server/middleware/httpLogger.js +++ b/server/middleware/httpLogger.js @@ -1,5 +1,5 @@ const httpLogger = - ({ timeLogs = false }) => + ({ enableTimestamps = false }) => (req, res, next) => { // Capture the original res.end to log response status const originalEnd = res.end; @@ -8,7 +8,7 @@ const httpLogger = // 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} ${timeLogs ? `@ ${new Date().toLocaleTimeString("en-US", { hour12: true })}` : ""}`.trim() + `\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 From da4ecf6ec3fff10b68854c2c1629ee46631e80f0 Mon Sep 17 00:00:00 2001 From: angelplusultra Date: Fri, 26 Sep 2025 09:27:53 -0700 Subject: [PATCH 5/6] Make HTTP Logger only mount in development and environment flag is enabled. --- collector/.env.example | 5 +++++ collector/.gitignore | 3 +++ collector/index.js | 12 ++++++++++-- server/.env.example | 7 ++++++- server/index.js | 13 ++++++++++--- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/collector/.env.example b/collector/.env.example index ea4aba395d6..63daf46cf00 100644 --- a/collector/.env.example +++ b/collector/.env.example @@ -1 +1,6 @@ # Placeholder .env file for collector runtime + +# This enables HTTP request/response logging in development. Set value to truthy string to enable, leave empty value or comment out to disable +# ENABLE_HTTP_LOGGER="" +# This enables timestamps for the HTTP Logger. Set value to true to enable, leave empty or comment out to disable +# ENABLE_HTTP_LOGGER_TIMESTAMPS="" \ No newline at end of file diff --git a/collector/.gitignore b/collector/.gitignore index 57436cefda4..86ced2aa050 100644 --- a/collector/.gitignore +++ b/collector/.gitignore @@ -4,3 +4,6 @@ yarn-error.log !yarn.lock outputs scripts +.env.development +.env.production +.env.test diff --git a/collector/index.js b/collector/index.js index e0b53300f2e..7955ce99bca 100644 --- a/collector/index.js +++ b/collector/index.js @@ -19,8 +19,16 @@ const { httpLogger } = require("./middleware/httpLogger"); const app = express(); const FILE_LIMIT = "3GB"; -if (process.env.NODE_ENV === "development") { - app.use(httpLogger({ timeLogs: false })); +// Only log HTTP requests in development mode and if the ENABLE_HTTP_LOGGER environment variable is set to true +if ( + process.env.NODE_ENV === "development" && + !!process.env.ENABLE_HTTP_LOGGER +) { + app.use( + httpLogger({ + enableTimestamps: !!process.env.ENABLE_HTTP_LOGGER_TIMESTAMPS, + }) + ); } app.use(cors({ origin: true })); app.use( diff --git a/server/.env.example b/server/.env.example index c60319ab6ab..cebb4cdbd56 100644 --- a/server/.env.example +++ b/server/.env.example @@ -367,4 +367,9 @@ TTS_PROVIDER="native" # Runtime flags for built-in pupeeteer Chromium instance # This is only required on Linux machines running AnythingLLM via Docker # and do not want to use the --cap-add=SYS_ADMIN docker argument -# ANYTHINGLLM_CHROMIUM_ARGS="--no-sandbox,--disable-setuid-sandbox" \ No newline at end of file +# ANYTHINGLLM_CHROMIUM_ARGS="--no-sandbox,--disable-setuid-sandbox" + +# This enables HTTP request/response logging in development. Set value to truthy string to enable, leave empty value or comment out to disable +# ENABLE_HTTP_LOGGER="" +# This enables timestamps for the HTTP Logger. Set value to true to enable, leave empty or comment out to disable +# ENABLE_HTTP_LOGGER_TIMESTAMPS="" \ No newline at end of file diff --git a/server/index.js b/server/index.js index 29e6625f8a7..e578c3f6405 100644 --- a/server/index.js +++ b/server/index.js @@ -34,9 +34,16 @@ const app = express(); const apiRouter = express.Router(); const FILE_LIMIT = "3GB"; -// Only log HTTP requests in development mode -if (process.env.NODE_ENV === "development") { - app.use(httpLogger({ timeLogs: false })); +// Only log HTTP requests in development mode and if the ENABLE_HTTP_LOGGER environment variable is set to true +if ( + process.env.NODE_ENV === "development" && + !!process.env.ENABLE_HTTP_LOGGER +) { + app.use( + httpLogger({ + enableTimestamps: !!process.env.ENABLE_HTTP_LOGGER_TIMESTAMPS, + }) + ); } app.use(cors({ origin: true })); app.use(bodyParser.text({ limit: FILE_LIMIT })); From 842adc18abfd28da71218c7d9b720430b08f45c1 Mon Sep 17 00:00:00 2001 From: angelplusultra Date: Fri, 26 Sep 2025 09:30:36 -0700 Subject: [PATCH 6/6] Update .env.example to clarify HTTP Logger configuration comments --- server/.env.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/.env.example b/server/.env.example index cebb4cdbd56..6cc1a6ed99a 100644 --- a/server/.env.example +++ b/server/.env.example @@ -369,7 +369,7 @@ TTS_PROVIDER="native" # and do not want to use the --cap-add=SYS_ADMIN docker argument # ANYTHINGLLM_CHROMIUM_ARGS="--no-sandbox,--disable-setuid-sandbox" -# This enables HTTP request/response logging in development. Set value to truthy string to enable, leave empty value or comment out to disable +# This enables HTTP request/response logging in development. Set value to a truthy string to enable, leave empty value or comment out to disable. # ENABLE_HTTP_LOGGER="" -# This enables timestamps for the HTTP Logger. Set value to true to enable, leave empty or comment out to disable +# This enables timestamps for the HTTP Logger. Set value to a truthy string to enable, leave empty value or comment out to disable. # ENABLE_HTTP_LOGGER_TIMESTAMPS="" \ No newline at end of file