@@ -146,6 +146,59 @@ async function getResolverMap(): Promise<ResolverFiles | null | undefined> {
146
146
interface RpcConfig {
147
147
onError ?: ( error : Error ) => void
148
148
formatError ?: ( error : Error ) => Error
149
+ logging ?: {
150
+ /**
151
+ * allowList Represents the list of routes for which logging should be enabled
152
+ * If allowList is defined then only those routes will be logged
153
+ */
154
+ allowList ?: string [ ]
155
+ /**
156
+ * blockList Represents the list of routes for which logging should be disabled
157
+ * If blockList is defined then all routes except those will be logged
158
+ */
159
+ blockList ?: string [ ]
160
+ /**
161
+ * verbose Represents the flag to enable/disable logging
162
+ * If verbose is true then Blitz RPC will log the input and output of each resolver
163
+ */
164
+ verbose ?: boolean
165
+ /**
166
+ * disablelevel Represents the flag to enable/disable logging for a particular level
167
+ */
168
+ disablelevel ?: "debug" | "info"
169
+ }
170
+ }
171
+
172
+ function isBlitzRPCVerbose ( resolverName : string , config : RpcConfig , level : string ) {
173
+ // blitz rpc is by default verbose - to keep current behaviour
174
+ if ( ! config . logging ) {
175
+ return true
176
+ }
177
+ //if logging exists and verbose is not defined then default to true
178
+ if ( config . logging && ! ( "verbose" in config . logging ) ) {
179
+ return true
180
+ }
181
+ const isLevelDisabled = config . logging ?. disablelevel === level
182
+ if ( config . logging ?. verbose ) {
183
+ // If allowList array is defined then allow only those routes in allowList
184
+ if ( config . logging ?. allowList ) {
185
+ if ( config . logging ?. allowList ?. includes ( resolverName ) && ! isLevelDisabled ) {
186
+ return true
187
+ }
188
+ }
189
+ // If blockList array is defined then allow all routes except those in blockList
190
+ if ( config . logging ?. blockList ) {
191
+ if ( ! config . logging ?. blockList ?. includes ( resolverName ) && ! isLevelDisabled ) {
192
+ return true
193
+ }
194
+ }
195
+ // if both allowList and blockList are not defined, then allow all routes
196
+ if ( ! config . logging ?. allowList && ! config . logging ?. blockList && ! isLevelDisabled ) {
197
+ return true
198
+ }
199
+ return false
200
+ }
201
+ return false
149
202
}
150
203
151
204
export function rpcHandler ( config : RpcConfig ) {
@@ -159,10 +212,11 @@ export function rpcHandler(config: RpcConfig) {
159
212
160
213
const relativeRoutePath = ( req . query . blitz as string [ ] ) ?. join ( "/" )
161
214
const routePath = "/" + relativeRoutePath
215
+ const resolverName = routePath . replace ( / ( \/ a p i \/ r p c ) ? \/ / , "" )
162
216
163
217
const log = baseLogger ( ) . getSubLogger ( {
164
218
name : "blitz-rpc" ,
165
- prefix : [ routePath . replace ( / ( \/ a p i \/ r p c ) ? \/ / , "" ) + "()" ] ,
219
+ prefix : [ resolverName + "()" ] ,
166
220
} )
167
221
const customChalk = new chalk . Instance ( {
168
222
level : log . settings . type === "json" ? 0 : chalk . level ,
@@ -213,11 +267,16 @@ export function rpcHandler(config: RpcConfig) {
213
267
? parse ( `${ req . query . meta } ` )
214
268
: undefined ,
215
269
} )
216
- log . info ( customChalk . dim ( "Starting with input:" ) , data ? data : JSON . stringify ( data ) )
270
+ if ( isBlitzRPCVerbose ( resolverName , config , "info" ) ) {
271
+ log . info ( customChalk . dim ( "Starting with input:" ) , data ? data : JSON . stringify ( data ) )
272
+ }
217
273
const startTime = Date . now ( )
218
274
const result = await resolver ( data , ( res as any ) . blitzCtx )
219
275
const resolverDuration = Date . now ( ) - startTime
220
- log . info ( customChalk . dim ( "Result:" ) , result ? result : JSON . stringify ( result ) )
276
+
277
+ if ( isBlitzRPCVerbose ( resolverName , config , "debug" ) ) {
278
+ log . debug ( customChalk . dim ( "Result:" ) , result ? result : JSON . stringify ( result ) )
279
+ }
221
280
222
281
const serializerStartTime = Date . now ( )
223
282
const serializedResult = superjsonSerialize ( result )
@@ -231,21 +290,26 @@ export function rpcHandler(config: RpcConfig) {
231
290
result : serializedResult . meta ,
232
291
} ,
233
292
} )
234
- log . debug (
235
- customChalk . dim (
236
- `Next.js serialization:${ prettyMs ( Date . now ( ) - nextSerializerStartTime ) } ` ,
237
- ) ,
238
- )
293
+
294
+ if ( isBlitzRPCVerbose ( resolverName , config , "debug" ) ) {
295
+ log . debug (
296
+ customChalk . dim (
297
+ `Next.js serialization:${ prettyMs ( Date . now ( ) - nextSerializerStartTime ) } ` ,
298
+ ) ,
299
+ )
300
+ }
301
+
239
302
const serializerDuration = Date . now ( ) - serializerStartTime
240
303
const duration = Date . now ( ) - startTime
241
-
242
- log . debug (
243
- customChalk . dim (
244
- `Finished: resolver:${ prettyMs ( resolverDuration ) } serializer:${ prettyMs (
245
- serializerDuration ,
246
- ) } total:${ prettyMs ( duration ) } `,
247
- ) ,
248
- )
304
+ if ( isBlitzRPCVerbose ( resolverName , config , "info" ) ) {
305
+ log . info (
306
+ customChalk . dim (
307
+ `Finished: resolver:${ prettyMs ( resolverDuration ) } serializer:${ prettyMs (
308
+ serializerDuration ,
309
+ ) } total:${ prettyMs ( duration ) } `,
310
+ ) ,
311
+ )
312
+ }
249
313
newLine ( )
250
314
251
315
return
0 commit comments