diff --git a/README.md b/README.md index 382f80f..0b340e8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# slogctx +# clog -[![Go Reference](https://pkg.go.dev/badge/github.com/chainguard-dev/slogctx.svg)](https://pkg.go.dev/github.com/chainguard-dev/slogctx) +[![Go Reference](https://pkg.go.dev/badge/github.com/chainguard-dev/clog.svg)](https://pkg.go.dev/github.com/chainguard-dev/clog) Context aware slog @@ -17,14 +17,14 @@ This approach is heavily inspired by ```go func main() { - log := slogctx.New(slog.Default).With("a", "b") - ctx := slogctx.WithLogger(log) + log := clog.New(slog.Default).With("a", "b") + ctx := clog.WithLogger(context.Background(), log) // Grab logger from context and use - slogctx.FromContext(ctx).With("foo", "bar").Infof("hello world") + clog.FromContext(ctx).With("foo", "bar").Infof("hello world") // Package level context loggers are also aware - slogctx.ErrorContext(ctx, "asdf") + clog.ErrorContext(ctx, "asdf") } ``` @@ -44,7 +44,7 @@ func TestFoo(t *testing.T) { for _, tc := range []string{"a", "b"} { t.Run(tc, func(t *testing.T) { - slogctx.FromContext(ctx).Infof("hello world") + clog.FromContext(ctx).Infof("hello world") }) } } @@ -65,7 +65,7 @@ $ go test -v ./examples/logger --- PASS: TestLog/a (0.00s) --- PASS: TestLog/b (0.00s) PASS -ok github.com/chainguard-dev/slogctx/examples/logger +ok github.com/chainguard-dev/clog/examples/logger ``` ### Context Handler @@ -74,18 +74,18 @@ The context Handler can be used to insert values from the context. ```go func init() { - slog.SetDefault(slog.New(slogctx.NewHandler(slog.NewTextHandler(os.Stdout, nil)))) + slog.SetDefault(slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, nil)))) } func main() { ctx := context.Background() - ctx = slogctx.WithValue(ctx, "foo", "bar") + ctx = clog.WithValue(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) - slogctx.Errorf(ctx, "hello %s", "world") + clog.Errorf(ctx, "hello %s", "world") } ``` diff --git a/example_test.go b/example_test.go index e687fb7..393b7c5 100644 --- a/example_test.go +++ b/example_test.go @@ -1,22 +1,22 @@ -package slogctx_test +package clog_test import ( "context" "log/slog" "os" - "github.com/chainguard-dev/slogctx" - "github.com/chainguard-dev/slogctx/slogtest" + "github.com/chainguard-dev/clog" + "github.com/chainguard-dev/clog/slogtest" ) func ExampleHandler() { - log := slog.New(slogctx.NewHandler(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + log := slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ // Remove time for repeatable results ReplaceAttr: slogtest.RemoveTime, }))) ctx := context.Background() - ctx = slogctx.WithValues(ctx, "foo", "bar") + ctx = clog.WithValues(ctx, "foo", "bar") log.InfoContext(ctx, "hello world", slog.Bool("baz", true)) // Output: @@ -24,19 +24,19 @@ func ExampleHandler() { } func ExampleLogger() { - log := slogctx.NewLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + log := clog.NewLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ // Remove time for repeatable results ReplaceAttr: slogtest.RemoveTime, }))) log = log.With("a", "b") - ctx := slogctx.WithLogger(context.Background(), log) + ctx := clog.WithLogger(context.Background(), log) // Grab logger from context and use // Note: this is a formatter aware method, not an slog.Attr method. - slogctx.FromContext(ctx).With("foo", "bar").Infof("hello %s", "world") + clog.FromContext(ctx).With("foo", "bar").Infof("hello %s", "world") // Package level context loggers are also aware - slogctx.ErrorContext(ctx, "asdf", slog.Bool("baz", true)) + clog.ErrorContext(ctx, "asdf", slog.Bool("baz", true)) // Output: // level=INFO msg="hello world" a=b foo=bar diff --git a/examples/handler/main.go b/examples/handler/main.go index 8ca3443..4265b6d 100644 --- a/examples/handler/main.go +++ b/examples/handler/main.go @@ -5,20 +5,20 @@ import ( "log/slog" "os" - "github.com/chainguard-dev/slogctx" + "github.com/chainguard-dev/clog" ) func init() { - slog.SetDefault(slog.New(slogctx.NewHandler(slog.NewTextHandler(os.Stdout, nil)))) + slog.SetDefault(slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, nil)))) } func main() { ctx := context.Background() - ctx = slogctx.WithValues(ctx, "foo", "bar") + 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) - slogctx.Errorf("hello %s", "world") + clog.Errorf("hello %s", "world") } diff --git a/examples/logger/main.go b/examples/logger/main.go index 496ffd2..6d40b05 100644 --- a/examples/logger/main.go +++ b/examples/logger/main.go @@ -4,16 +4,16 @@ import ( "context" "log/slog" - "github.com/chainguard-dev/slogctx" + "github.com/chainguard-dev/clog" ) func main() { - log := slogctx.NewLogger(slog.Default()).With("a", "b") - ctx := slogctx.WithLogger(context.Background(), log) + log := clog.NewLogger(slog.Default()).With("a", "b") + ctx := clog.WithLogger(context.Background(), log) // Grab logger from context and use - slogctx.FromContext(ctx).With("foo", "bar").Infof("hello world") + clog.FromContext(ctx).With("foo", "bar").Infof("hello world") // Package level context loggers are also aware - slogctx.ErrorContext(ctx, "asdf") + clog.ErrorContext(ctx, "asdf") } diff --git a/examples/logger/main_test.go b/examples/logger/main_test.go index e3824a1..15d5411 100644 --- a/examples/logger/main_test.go +++ b/examples/logger/main_test.go @@ -3,8 +3,8 @@ package main import ( "testing" - "github.com/chainguard-dev/slogctx" - "github.com/chainguard-dev/slogctx/slogtest" + "github.com/chainguard-dev/clog" + "github.com/chainguard-dev/clog/slogtest" ) func TestFoo(t *testing.T) { @@ -12,7 +12,7 @@ func TestFoo(t *testing.T) { for _, tc := range []string{"a", "b"} { t.Run(tc, func(t *testing.T) { - slogctx.FromContext(ctx).Infof("hello world") + clog.FromContext(ctx).Infof("hello world") }) } } diff --git a/go.mod b/go.mod index d9f27c0..22b823f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/chainguard-dev/slogctx +module github.com/chainguard-dev/clog go 1.21.2 diff --git a/handler.go b/handler.go index 2edddff..ed9d595 100644 --- a/handler.go +++ b/handler.go @@ -1,4 +1,4 @@ -package slogctx +package clog import ( "context" diff --git a/handler_test.go b/handler_test.go index b433588..7c03be6 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1,4 +1,4 @@ -package slogctx +package clog import ( "bytes" diff --git a/log.go b/log.go index 999d27c..9004375 100644 --- a/log.go +++ b/log.go @@ -1,4 +1,4 @@ -package slogctx +package clog import ( "context" diff --git a/logger.go b/logger.go index 00f4c99..37365c6 100644 --- a/logger.go +++ b/logger.go @@ -1,4 +1,4 @@ -package slogctx +package clog import ( "context" diff --git a/logger_test.go b/logger_test.go index 544efc4..c48fed7 100644 --- a/logger_test.go +++ b/logger_test.go @@ -1,4 +1,4 @@ -package slogctx +package clog import ( "bytes" @@ -76,7 +76,7 @@ func TestLoggerFromContext(t *testing.T) { b.Reset() - t.Run("slogctx.Info", func(t *testing.T) { + t.Run("clog.Info", func(t *testing.T) { InfoContext(ctx, "") var got map[string]any if err := json.Unmarshal(b.Bytes(), &got); err != nil { @@ -108,7 +108,7 @@ func TestLoggerPC(t *testing.T) { t.Fatal(err) } // Knowing that the PC is from this test is good enough. - want := fmt.Sprintf("github.com/chainguard-dev/slogctx.%s", t.Name()) + want := fmt.Sprintf("github.com/chainguard-dev/clog.%s", t.Name()) if got.Source.Function != want { t.Errorf("want %v, got %v", want, got) } diff --git a/slogtest/slogtest.go b/slogtest/slogtest.go index 3cce940..c56bd72 100644 --- a/slogtest/slogtest.go +++ b/slogtest/slogtest.go @@ -5,7 +5,7 @@ import ( "io" "log/slog" - "github.com/chainguard-dev/slogctx" + "github.com/chainguard-dev/clog" ) var ( @@ -27,13 +27,13 @@ type Logger interface { } // TestLogger gets a logger to use in unit and end to end tests -func TestLogger(t Logger) *slogctx.Logger { - return slogctx.New(slog.NewTextHandler(&logAdapter{l: t}, nil)) +func TestLogger(t Logger) *clog.Logger { + return clog.New(slog.NewTextHandler(&logAdapter{l: t}, nil)) } // TestContextWithLogger returns a context with a logger to be used in tests func TestContextWithLogger(t Logger) context.Context { - return slogctx.WithLogger(context.Background(), TestLogger(t)) + return clog.WithLogger(context.Background(), TestLogger(t)) } // RemoveTime removes the top-level time attribute.