I'm using NodeJS with Express. The app is deployed to Google Cloud Run. Currently the logs look, so I have a request log coming from Google, and any console.logs that I emit on purpose. With large number of requests, these can get mixed up.
I've noticed that simply by changing console.log({ hello: 'world' })
to console.log(JSON.stringify({ hello: 'world' }))
, the payload is automatically added as jsonPayload
to the log record.
Is there a way/library for me to automatically "nest" the console logs under each API request? Or in general, what is "the best" approach for Express + Cloud Run logging?
As a recommendation, there are some approaches that you can try (Bunyan, Winston and API Library) for the logging strategy that may work on what you need:
Bunyan logging is more verbose than Winston, which could be a consideration if cost is an issue.
Winston allows you to create structured logs with custom fields like request IDs, timestamps, and your actual log message.
Attaching here the documentation from an article with simplified steps that you can check for reference.