Chapter 9. Structured Logging and Tracing

This chapter is about structured logging: adding metadata to the logs of your application so you have more context when you read them and can group related logs together or filter them. The metadata can include, for example, the log severity level and relevant business attributes.

You’ll learn how your application logs go from your container to Cloud Logging, and how you can improve them with a log level (debug, info, warning, error, fatal, or panic). I’ll also show you how to make them even more useful with custom attributes.

On Google Cloud, logging is handled by Cloud Logging. It lets you create dashboards and interactively build queries to find logs, and it shows a histogram with log activity.

In a production situation, you’ll want to correlate logs with a request so you can easily view all logs that were written in the context of handling that single request. Similarly, if you handle a request and you need to call another Cloud Run service, you’ll want to see the logs of the first request and the second request grouped together. I’ll show you how that works by propagating a trace header to your downstream services.

Logging on Cloud Run

Cloud Run captures logs from your container and forwards them to Cloud Logging. This is what Cloud Run captures:

  • Container output streams—standard out and standard error of the container process

  • Every line written to files in the /var/log directory (or subdirectories)

  • Syslog—if you use the syslog ...

Get Building Serverless Applications with Google Cloud Run now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.