| 1 | # `clog/gcp`: structured logging for Google Cloud using [`slog`](https://pkg.go.dev/log/slog) |
| 2 | |
| 3 | Contrary to the |
| 4 | [documented "standard" approach for logging](https://cloud.google.com/logging/docs/setup/go), |
| 5 | this doesn't use any third-party logging package for logging. |
| 6 | |
| 7 | Instead, it relies on Google Cloud's support for ingesting structured logs by |
| 8 | simply printing JSON to stderr. |
| 9 | |
| 10 | This method of emitting structured logs is supported by: |
| 11 | |
| 12 | - [Cloud Run](https://cloud.google.com/run/docs/logging#using-json) |
| 13 | - [Kubernetes Engine](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields) |
| 14 | - [Cloud Functions](https://cloud.google.com/functions/docs/monitoring/logging#writing_structured_logs) |
| 15 | - [App Engine](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields) |
| 16 | (standard and flexible) |
| 17 | - and in other products, using the |
| 18 | [Cloud Logging agent](https://cloud.google.com/logging/docs/agent/logging) and |
| 19 | [Ops agent](https://cloud.google.com/logging/docs/agent/ops-agent). |
| 20 | |
| 21 | ## Basic Usage |
| 22 | |
| 23 | To use this, underscore-import `gcp/init`, which will configure `slog` to use |
| 24 | the GCP-optimized JSON handler for all log messages: |
| 25 | |
| 26 | Then when you use `slog`, all log messages will be output in JSON format to |
| 27 | standard error, which is automatically ingested by Cloud Logging. |
| 28 | |
| 29 | ```go |
| 30 | import _ "github.com/chainguard-dev/clog/gcp/init" |
| 31 | |
| 32 | ... |
| 33 | |
| 34 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 35 | slog.InfoContext(r.Context(), "my message", |
| 36 | "mycount", 42, |
| 37 | "mystring", "myvalue", |
| 38 | ) |
| 39 | }) |
| 40 | ``` |
| 41 | |
| 42 | This logs the message, with the additional structured logging fields in Cloud |
| 43 | Logging: |
| 44 | |
| 45 | <table><tr><td><img src="basic.png" width=50%/></td></tr></table> |
| 46 | |
| 47 | ## Correlating Logs with Requests |
| 48 | |
| 49 | You can also use this to correlate log lines with the request that generated |
| 50 | them, by associating the log message with the request's trace context header. |
| 51 | |
| 52 | ```go |
| 53 | import "github.com/chainguard-dev/clog/gcp" |
| 54 | |
| 55 | ... |
| 56 | |
| 57 | http.Handle("/", gcp.WithCloudTraceContext(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | slog.InfoContext(r.Context(), "my message", |
| 59 | "mycount", 42, |
| 60 | "mystring", "myvalue", |
| 61 | ) |
| 62 | }))) |
| 63 | ``` |
| 64 | |
| 65 | This logs the message, associated with the request's trace, in Cloud Logging: |
| 66 | |
| 67 | <table><tr><td><img src="correlated.png" width=50%/></td></tr></table> |
| 68 | |
| 69 | Other logs with the same `trace` attribute are generated by the same incoming |
| 70 | request. |
| 71 | |
| 72 | See https://cloud.google.com/trace/docs/trace-log-integration for more |
| 73 | information. |
| 74 | |
| 75 | ## Critical Logging |
| 76 | |
| 77 | Cloud Logging supports a **CRITICAL** logging level, which doesn't map cleanly |
| 78 | to `slog`'s built-in levels. |
| 79 | |
| 80 | To log at this level: |
| 81 | |
| 82 | ```go |
| 83 | slog.Log(ctx, gcp.LevelCritical, "I have a bad feeling about this...") |
| 84 | ``` |
| 85 | |
| 86 | See `./cmd/example` for a deployable example. |
| 87 | |
| 88 | --- |
| 89 | |
| 90 | This repo is forked from https://github.com/remko/cloudrun-slog, which |
| 91 | originated this idea and implementation. |