@@ -6,6 +6,7 @@ import { ServiceError } from '../error';
66import type { AnyServiceLocals , RequestWithApp , ServiceExpress , ServiceLocals } from '../types' ;
77import type { ServiceHandler } from '../express-app/types' ;
88import { ConfigurationSchema } from '../config/schema' ;
9+ import { getNodeEnv } from '../env' ;
910
1011const LOG_PREFS = Symbol ( 'Logging information' ) ;
1112
@@ -117,18 +118,22 @@ export function loggerMiddleware<
117118 histogram : Histogram ,
118119 config ?: ConfigurationSchema [ 'logging' ] ,
119120) : RequestHandler {
121+ const nonProd = getNodeEnv ( ) !== 'production' ;
120122 const { logger, service } = app . locals ;
121123 return function gblogger ( req , res , next ) {
124+ const logResponse =
125+ config ?. logResponseBody || ( nonProd && req . headers [ 'x-log' ] ?. includes ( 'res' ) ) ;
126+ const logRequest = config ?. logRequestBody || ( nonProd && req . headers [ 'x-log' ] ?. includes ( 'req' ) ) ;
122127 const prefs : LogPrefs = {
123128 start : process . hrtime ( ) ,
124- logRequests : config ?. logRequestBody ,
125- chunks : config ?. logResponseBody ? [ ] : undefined ,
129+ logRequests : logRequest ,
130+ chunks : logResponse ? [ ] : undefined ,
126131 logged : false ,
127132 } ;
128133
129134 ( res . locals as WithLogPrefs ) [ LOG_PREFS ] = prefs ;
130135
131- if ( config ?. logResponseBody ) {
136+ if ( logResponse ) {
132137 // res is a read-only stream, so the only way to intercept response
133138 // data is to monkey-patch.
134139 const oldWrite = res . write ;
0 commit comments