Categorygithub.com/chainguard-dev/clog
modulepackage
1.5.0
Repository: https://github.com/chainguard-dev/clog.git
Documentation: pkg.go.dev

# README

👞 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.

# Packages

No description provided by the author
No description provided by the author
Package slag provides a method for setting the log level from the command line.
Package slogtest provides utilities for emitting test logs using clog.

# Functions

Debug calls Debug on the default logger.
DebugContext calls DebugContext on the context logger.
DebugContextf calls DebugContextf on the context logger.
Debugf calls Debugf on the default logger.
DefaultLogger returns a new logger that uses the default [slog.Logger].
Error calls Error on the default logger.
ErrorContext calls ErrorContext on the context logger.
ErrorContextf calls ErrorContextf on the context logger.
Errorf calls Errorf on the default logger.
Fatal calls Error on the default logger, then exits.
FatalContext calls ErrorContext on the context logger, then exits.
FatalContextf calls ErrorContextf on the context logger, then exits.
Fatalf calls Errorf on the default logger, then exits.
No description provided by the author
Info calls Info on the default logger.
InfoContext calls InfoContext on the context logger.
InfoContextf calls InfoContextf on the context logger.
Infof calls Infof on the default logger.
New returns a new logger that wraps the given [slog.Handler].
NewHandler configures a new context aware slog handler.
NewLogger returns a new logger that wraps the given [slog.Logger] with the default context.
NewLoggerWithContext returns a new logger that wraps the given [slog.Logger].
New returns a new logger that wraps the given [slog.Handler].
Warn calls Warn on the default logger.
WarnContext calls WarnContext on the context logger.
WarnContextf calls WarnContextf on the context logger.
Warnf calls Warnf on the default logger.
With calls [Logger.With] on the default logger.
No description provided by the author
With returns a new context with the given values.

# Structs

Handler is a slog.Handler that adds context values to the log record.
Logger implements a wrapper around [slog.Logger] that adds formatter functions (e.g.