|
1 | 1 | # gslog
|
2 |
| -An slog Handler for Google Cloud Logging |
| 2 | + |
| 3 | + |
| 4 | +[](http://godoc.org/github.com/maguro/gslog) |
| 5 | +[](https://goreportcard.com/report/github.com/maguro/gslog) |
| 6 | +[](https://codecov.io/gh/maguro/gslog) |
| 7 | +[](./LICENSE) |
| 8 | + |
| 9 | +A Google Cloud Logging [Handler](https://pkg.go.dev/log/slog#Handler) implementation |
| 10 | +for [slog](https://go.dev/blog/slog). |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +Critical level log records will be sent synchronously. |
| 15 | + |
| 16 | +## Install |
| 17 | + |
| 18 | +```sh |
| 19 | +go get m4o.io/gslog |
| 20 | +``` |
| 21 | + |
| 22 | +**Compatibility**: go >= 1.21 |
| 23 | + |
| 24 | +## Example Usage |
| 25 | + |
| 26 | +First create a [Google Cloud Logging](https://pkg.go.dev/cloud.google.com/go/logging) |
| 27 | +`logging.Client` to use throughout your application: |
| 28 | + |
| 29 | +```go |
| 30 | +ctx := context.Background() |
| 31 | +client, err := logging.NewClient(ctx, "my-project") |
| 32 | +if err != nil { |
| 33 | + // TODO: Handle error. |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Usually, you'll want to add log entries to a buffer to be periodically flushed |
| 38 | +(automatically and asynchronously) to the Cloud Logging service. Use the |
| 39 | +logger when creating the new `gslog.GcpHandler` which is passed to `slog.New()` |
| 40 | +to obtain a `slog`-based logger. |
| 41 | + |
| 42 | +```go |
| 43 | +loggger := client.Logger("my-log") |
| 44 | + |
| 45 | +h := gslog.NewGcpHandler(loggger) |
| 46 | +l := slog.New(h) |
| 47 | + |
| 48 | +l.Info("How now brown cow?") |
| 49 | +``` |
| 50 | + |
| 51 | +Writing critical, or higher, log level entries will be sent synchronously. |
| 52 | + |
| 53 | +```go |
| 54 | +l.Log(context.Background(), gslog.LevelCritical, "Danger, Will Robinson!") |
| 55 | +``` |
| 56 | + |
| 57 | +Close your client before your program exits, to flush any buffered log entries. |
| 58 | + |
| 59 | +```go |
| 60 | +err = client.Close() |
| 61 | +if err != nil { |
| 62 | + // TODO: Handle error. |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Logger Configuration Options |
| 67 | + |
| 68 | +Creating a Google Cloud Logging [Handler](https://pkg.go.dev/log/slog#Handler) using `gslog.NewGcpHandler(logger, ...options)` accepts the |
| 69 | +following options: |
| 70 | + |
| 71 | +| Configuration option | Arguments | Description | |
| 72 | +|----------------------------------------|:------------------:|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 73 | +| `gslog.WithLogLeveler(leveler)` | `slog.Leveler` | Specifies the `slog.Leveler` for logging. Explicitly setting the log level here takes precedence over the other options. | |
| 74 | +| `gslog.WithLogLevelFromEnvVar(envVar)` | `string` | Specifies the log level for logging comes from tne environmental variable specified by the key. | |
| 75 | +| `gslog.WithDefaultLogLeveler()` | `slog.Leveler` | Specifies the default `slog.Leveler` for logging. | |
| 76 | +| `gslog.WithSourceAdded()` | | Causes the handler to compute the source code position of the log statement and add a `slog.SourceKey` attribute to the output. | |
| 77 | +| `gslog.WithLabels()` | | Adds any labels found in the context to the `logging.Entry`'s `Labels` field. | |
| 78 | +| `gslog.WithReplaceAttr(mapper)` | `gslog.AttrMapper` | Specifies an attribute mapper used to rewrite each non-group attribute before it is logged. | |
| 79 | +| `otel.WithOtelBaggage()` | | Directs that the `slog.Handler` to include [OpenTelemetry baggage](https://opentelemetry.io/docs/concepts/signals/baggage/). The `baggage.Baggage` is obtained from the context, if available, and added as attributes. | |
| 80 | +| `otel.WithOtelTracing()` | | Directs that the `slog.Handler` to include [OpenTelemetry tracing](https://opentelemetry.io/docs/concepts/signals/traces/). Tracing information is obtained from the `trace.SpanContext` stored in the context, if provided. | |
| 81 | +| `k8s.WithPodinfoLabels(root)` | `string` | Directs that the `slog.Handler` to include labels from the [Kubernetes Downward API](https://kubernetes.io/docs/concepts/workloads/pods/downward-api/) podinfo `labels` file. The labels file is expected to be found in the directory specified by root and MUST be named "labels", per the Kubernetes Downward API for Pods. | |
0 commit comments