https://github.com/ronny/clog
Google Cloud Logging adapter for `log/slog`.
https://github.com/ronny/clog
cloud-logging cloud-run go golang google-cloud google-cloud-platform logging slog structured-logging
Last synced: 10 days ago
JSON representation
Google Cloud Logging adapter for `log/slog`.
- Host: GitHub
- URL: https://github.com/ronny/clog
- Owner: ronny
- License: bsd-3-clause
- Created: 2024-04-25T14:42:01.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-29T05:00:36.000Z (over 1 year ago)
- Last Synced: 2024-06-19T11:31:37.458Z (over 1 year ago)
- Topics: cloud-logging, cloud-run, go, golang, google-cloud, google-cloud-platform, logging, slog, structured-logging
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# clog
[](https://pkg.go.dev/github.com/ronny/clog)
A lightweight [`log/slog.JSONHandler`](https://pkg.go.dev/log/slog#JSONHandler)
wrapper that adapts the fields to the [Google Cloud Logging structured log
format](https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields).
The intended use case is Cloud Run, but it should work in similar environments
where logs are emitted to stdout/stderr and automatically picked up by Cloud
Logging (e.g. App Engine, Cloud Functions, GKE).
## Features
- Lightweight. The handler merely reformats/renames the structured JSON log
fields. It's still [`log/slog.JSONHandler`](https://pkg.go.dev/log/slog#JSONHandler)
under the hood. It does NOT send logs to Cloud Logging directly (e.g. using
the Cloud SDK).
- Tracing. A tracing middleware is provided to automatically extract tracing
information from `traceparent` or `X-Cloud-Trace-Context` HTTP request header,
and attaches it to the request context. The Handler automatically includes any
tracing information as log attributes.
- Custom levels as supported by Google Cloud Logging, e.g. CRITICAL and NOTICE.
## Usage
```go
import (
"log/slog"
"cloud.google.com/go/compute/metadata"
"github.com/ronny/clog"
)
func main() {
projectID, err := metadata.ProjectID()
if err != nil {
panic(err)
}
logger := slog.New(
clog.NewHandler(os.Stderr, clog.HandlerOptions{
Level: clog.LevelInfo,
GoogleProjectID: projectID,
}),
)
slog.SetDefault(logger)
mux := http.NewServeMux()
mux.Handle("POST /warn", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// ⚠️ The log will have tracing attrs since we're using
// trace.Middleware below (assuming trace header is in the request):
// "logging.googleapis.com/trace"
// "logging.googleapis.com/spanId"
// "logging.googleapis.com/traceSampled"
slog.WarnContext(ctx, "flux capacitor is too warm",
"mycount", 42,
)
}))
mux.Handle("POST /critical", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// ⚠️ Custom level CRITICAL
slog.Log(ctx, clog.LevelCritical, "flux capacitor is on fire")
}))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("listening on port %s", port)
// ⚠️ `trace.Middleware` to make tracing information available in ctx in mux
// handlers.
if err := http.ListenAndServe(":"+port, trace.Middleware(mux)); err != nil {
log.Fatal(err)
}
}
```
## Credits and acknowledgements
Thank you to Remko Tronçon for doing most of the hard work in
https://github.com/remko/cloudrun-slog which is the basis for this library.