Log request details

To debug my router, I’d like to console log all request details.

The example from:

https://docs.layer0.co/guides/debugging#section_cloud


  .get('/p/:productId', ({ cache }) => {
    proxy('origin', {
      // The presence of transformRequest and transformResponse ensure that proxying is done in serverless, not at the edge.
      transformRequest: (req) => {
        console.log('Request ID', req.headers['x-request-id'])
        // Log request properties that you want to troubleshoot.
      },
      transformResponse: (res, req) => {
        console.log('Response for request ID', req.headers['x-request-id'], 'status code', res.statusCode)
        // Log response properties that you want to troubleshoot.
      }
    })
  })

Looks powerful and close to what I need, but I’d like to avoid having to add the console.log to every route.

How can I log the request object without adding console.logs to every route?

If the different routes you want to log are similar in signature (eg proxying from the same origin), you can just define the handler as a function to reuse on the different routes:

const debugHandler = ({ proxy }) => {
    proxy('origin', {
      // The presence of transformRequest and transformResponse ensure that proxying is done in serverless, not at the edge.
      transformRequest: (req) => {
        console.log('Request ID', req.headers['x-request-id'])
        // Log request properties that you want to troubleshoot.
      },
      transformResponse: (res, req) => {
        console.log('Response for request ID', req.headers['x-request-id'], 'status code', res.statusCode)
        // Log response properties that you want to troubleshoot.
      }
    })
  }

...

.get('/p/:productId', debugHandler)
.get('/c/:categoryId', debugHandler)

This will run everything through serverless instead of at the edge so surcharges will accrue. I would advise only doing it on the individual route you wish to debug as a temporary measure.