File tree Expand file tree Collapse file tree 7 files changed +90
-1
lines changed Expand file tree Collapse file tree 7 files changed +90
-1
lines changed Original file line number Diff line number Diff line change 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=""
Original file line number Diff line number Diff line change @@ -4,3 +4,6 @@ yarn-error.log
44! yarn.lock
55outputs
66scripts
7+ .env.development
8+ .env.production
9+ .env.test
Original file line number Diff line number Diff line change @@ -15,9 +15,21 @@ const { wipeCollectorStorage } = require("./utils/files");
1515const extensions = require ( "./extensions" ) ;
1616const { processRawText } = require ( "./processRawText" ) ;
1717const { verifyPayloadIntegrity } = require ( "./middleware/verifyIntegrity" ) ;
18+ const { httpLogger } = require ( "./middleware/httpLogger" ) ;
1819const app = express ( ) ;
1920const 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+ }
2133app . use ( cors ( { origin : true } ) ) ;
2234app . use (
2335 bodyParser . text ( { limit : FILE_LIMIT } ) ,
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff 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=""
Original file line number Diff line number Diff line change @@ -29,10 +29,22 @@ const { communityHubEndpoints } = require("./endpoints/communityHub");
2929const { agentFlowEndpoints } = require ( "./endpoints/agentFlows" ) ;
3030const { mcpServersEndpoints } = require ( "./endpoints/mcpServers" ) ;
3131const { mobileEndpoints } = require ( "./endpoints/mobile" ) ;
32+ const { httpLogger } = require ( "./middleware/httpLogger" ) ;
3233const app = express ( ) ;
3334const apiRouter = express . Router ( ) ;
3435const 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+ }
3648app . use ( cors ( { origin : true } ) ) ;
3749app . use ( bodyParser . text ( { limit : FILE_LIMIT } ) ) ;
3850app . use ( bodyParser . json ( { limit : FILE_LIMIT } ) ) ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments