1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-06 22:12:46 +00:00
No description
Find a file
Zack Crosley 01a02443da
fix: convert WARN string for cloud batch (#76)
### What

Convert WARN string to WARNING for compatibility with Cloud Batch

### Why

We have a service running in Cloud Batch which alerts on any warning
log. Digging into this, it appears Cloud Batch does not convert WARN ->
WARNING and thus interprets the unidentifiable log level as error.

Looking at logs for this service, the `jsonPayload.severity` is removed
for all log levels except for WARN, which ends up interpreted as an
ERROR level:
```
for level in DEBUG INFO WARN WARNING ERROR CRITICAL; do
  count=$(gcloud logging read "resource.type=\"batch.googleapis.com/Job\" labels.component=\"...\" jsonPayload.severity=\"${level}\"" \
    --project=... --limit=1 \
    --format="value(severity)" 2>/dev/null)
  echo "${level} -> ${count:-<no results>}"
done
DEBUG -> <no results>
INFO -> <no results>
WARN -> ERROR
WARNING -> <no results>
ERROR -> <no results>
CRITICAL -> <no results>
```

Signed-off-by: Zackary Crosley <zackary.crosley@chainguard.dev>
2026-06-25 14:02:33 -05:00
.chainguard Add source.yaml (#3) 2023-12-21 17:59:27 +01:00
.github fix(ci): add persist-credentials: false and zizmor pedantic config (#69) 2026-05-05 09:23:50 -07:00
examples add a flag option (#10) 2024-02-06 11:01:13 -05:00
gcp fix: convert WARN string for cloud batch (#76) 2026-06-25 14:02:33 -05:00
slag slag: support cobra's pflag interface (#24) 2024-08-11 11:59:37 -07:00
slogtest slogtest: demonstrate use of t.Context() (#41) 2025-08-30 21:11:13 -04:00
.gitignore Initial commit 2023-12-06 16:21:07 -07:00
example_test.go Preserve context in logger when calling FromContext. (#11) 2024-02-06 12:35:04 -05:00
go.mod slogtest: demonstrate use of t.Context() (#41) 2025-08-30 21:11:13 -04:00
go.sum Fix duplicate log keys from context. (#34) 2025-03-03 18:20:42 -05:00
handler.go Fix duplicate log keys from context. (#34) 2025-03-03 18:20:42 -05:00
handler_test.go rename to clog (#6) 2024-01-16 13:28:27 -05:00
LICENSE Initial commit 2023-12-06 16:21:07 -07:00
log.go add Fatal{Context}{f} (#8) 2024-02-06 11:03:48 -05:00
logger.go docs: fix doc comments for clog.Logger (#36) 2025-04-23 19:03:34 -04:00
logger_test.go Fix duplicate log keys from context. (#34) 2025-03-03 18:20:42 -05:00
README.md add GCP optimized slog package (#9) 2024-02-05 17:09:57 -05:00

👞 clog

Go Reference

Context-aware slog

slog was added in Go 1.21, so using this requires Go 1.21 or later.

Usage

Context Logger

The context Logger can be used to use Loggers from the context. This is sometimes preferred over the Context Handler, since this can make it easier to use different loggers in different contexts (e.g. testing).

This approach is heavily inspired by knative.dev/pkg/logging, but with zero dependencies outside the standard library (compare with pkg/logging's deps).

package main

import (
	"context"
	"log/slog"

	"github.com/chainguard-dev/clog"
)

func main() {
	// One-time setup
	log := clog.New(slog.Default().Handler()).With("a", "b")
	ctx := clog.WithLogger(context.Background(), log)

	f(ctx)
}

func f(ctx context.Context) {
	// Grab logger from context and use.
	log := clog.FromContext(ctx)
	log.Info("in f")

	// Add logging context and pass on.
	ctx = clog.WithLogger(ctx, log.With("f", "hello"))
	g(ctx)
}

func g(ctx context.Context) {
	// Grab logger from context and use.
	log := clog.FromContext(ctx)
	log.Info("in g")

	// Package level context loggers are also aware
	clog.ErrorContext(ctx, "asdf")
}

$ go run .
2009/11/10 23:00:00 INFO in f a=b
2009/11/10 23:00:00 INFO in g a=b f=hello
2009/11/10 23:00:00 ERROR asdf a=b f=hello

Testing

The slogtest package provides utilities to make it easy to create loggers that will use the native testing logging.

func TestFoo(t *testing.T) {
	ctx := slogtest.TestContextWithLogger(t)

	for _, tc := range []string{"a", "b"} {
		t.Run(tc, func(t *testing.T) {
			clog.FromContext(ctx).Infof("hello world")
		})
	}
}
$ go test -v ./examples/logger
=== RUN   TestLog
=== RUN   TestLog/a
=== NAME  TestLog
    slogtest.go:20: time=2023-12-12T18:42:53.020-05:00 level=INFO msg="hello world"

=== RUN   TestLog/b
=== NAME  TestLog
    slogtest.go:20: time=2023-12-12T18:42:53.020-05:00 level=INFO msg="hello world"

--- PASS: TestLog (0.00s)
    --- PASS: TestLog/a (0.00s)
    --- PASS: TestLog/b (0.00s)
PASS
ok      github.com/chainguard-dev/clog/examples/logger

Context Handler

The context Handler can be used to insert values from the context.

func init() {
	slog.SetDefault(slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, nil))))
}

func main() {
	ctx := context.Background()
	ctx = clog.WithValues(ctx, "foo", "bar")

	// Use slog package directly
	slog.InfoContext(ctx, "hello world", slog.Bool("baz", true))

	// glog / zap style (note: can't pass additional attributes)
	clog.ErrorContextf(ctx, "hello %s", "world")
}
$ go run .
time=2009-11-10T23:00:00.000Z level=INFO msg="hello world" baz=true foo=bar
time=2009-11-10T23:00:00.000Z level=ERROR msg="hello world" foo=bar

Google Cloud Platform support

This package also provides a GCP-optimized JSON handler for structured logging and trace attribution.

See ./gcp/README.md for details.