@@ -6,6 +6,7 @@ import { ServiceError } from '../error';
6
6
import type { AnyServiceLocals , RequestWithApp , ServiceExpress , ServiceLocals } from '../types' ;
7
7
import type { ServiceHandler } from '../express-app/types' ;
8
8
import { ConfigurationSchema } from '../config/schema' ;
9
+ import { getNodeEnv } from '../env' ;
9
10
10
11
const LOG_PREFS = Symbol ( 'Logging information' ) ;
11
12
@@ -117,18 +118,22 @@ export function loggerMiddleware<
117
118
histogram : Histogram ,
118
119
config ?: ConfigurationSchema [ 'logging' ] ,
119
120
) : RequestHandler {
121
+ const nonProd = getNodeEnv ( ) !== 'production' ;
120
122
const { logger, service } = app . locals ;
121
123
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' ) ) ;
122
127
const prefs : LogPrefs = {
123
128
start : process . hrtime ( ) ,
124
- logRequests : config ?. logRequestBody ,
125
- chunks : config ?. logResponseBody ? [ ] : undefined ,
129
+ logRequests : logRequest ,
130
+ chunks : logResponse ? [ ] : undefined ,
126
131
logged : false ,
127
132
} ;
128
133
129
134
( res . locals as WithLogPrefs ) [ LOG_PREFS ] = prefs ;
130
135
131
- if ( config ?. logResponseBody ) {
136
+ if ( logResponse ) {
132
137
// res is a read-only stream, so the only way to intercept response
133
138
// data is to monkey-patch.
134
139
const oldWrite = res . write ;
0 commit comments