DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

1 1

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.)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay