1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package main
import (
"context"
"flag"
"log/slog"
"os"
"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slag"
)
func main() {
var level slag.Level
flag.Var(&level, "log-level", "log level")
flag.Parse()
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: &level})))
log := clog.NewLogger(slog.Default()).With("a", "b")
ctx := clog.WithLogger(context.Background(), log)
// Grab logger from context and use
clog.FromContext(ctx).With("foo", "bar").Debugf("hello debug world")
clog.FromContext(ctx).With("info", true).Infof("hello info world")
clog.FromContext(ctx).With("warn", 42).Warnf("hello warn world")
// Package level context loggers are also aware
clog.ErrorContext(ctx, "hello error world")
}
|