DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

REST API logging in Google Cloud Run

As a response to my other post, as I use Google Cloud Run in production.

Logs tab

  • It's not exactly in the LOGS tab. You have to open Log viewer's page
  • JSON-like strings will be auto-transformed to JSON inside jsonPayload. Therefore, you can search like this,
severity>=DEFAULT
jsonPayload.req.method = "GET" 
Enter fullscreen mode Exit fullscreen mode
  • JSON strings are easier to log, therefore req.body and POST / PUT / PATCH as easier to log than req.query in GET / DELETE; however, it isn't impossible to pre-transform.
    • Another problem is req.query's serialization isn't very standardized. I use currently use rison-node.
    • I use GET requests for potential caching. But I didn't do any caching yet. Also, some GET are truly dynamic (e.g. /api/:dict/random).
export const logger = pino({
  serializers: {
    req(req) {
      return {
        method: req.method,
        url: req.url,
        version: req.headers['accept-version'],
        hostname: req.hostname,
        remoteAddress: req.ip,
        query: parseQuery(req.query),
      }
    },
  },
})
Enter fullscreen mode Exit fullscreen mode
  • At least in Fastify, you have to decide what to log, as by default, the logger doesn't log req.body. (It does log req.url, therefore, unparsed querystrings.)

Top comments (0)