mirror of
https://github.com/chainguard-dev/clog
synced 2026-07-06 22:12:46 +00:00
rename to clog (#6)
* rename to clog Signed-off-by: Jason Hall <jason@chainguard.dev> * fix example in README Signed-off-by: Jason Hall <jason@chainguard.dev> --------- Signed-off-by: Jason Hall <jason@chainguard.dev>
This commit is contained in:
parent
c071a43a42
commit
04bee692f7
12 changed files with 44 additions and 44 deletions
22
README.md
22
README.md
|
|
@ -1,6 +1,6 @@
|
|||
# slogctx
|
||||
# clog
|
||||
|
||||
[](https://pkg.go.dev/github.com/chainguard-dev/slogctx)
|
||||
[](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")
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,3 @@
|
|||
module github.com/chainguard-dev/slogctx
|
||||
module github.com/chainguard-dev/clog
|
||||
|
||||
go 1.21.2
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package slogctx
|
||||
package clog
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package slogctx
|
||||
package clog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
|
|
|||
2
log.go
2
log.go
|
|
@ -1,4 +1,4 @@
|
|||
package slogctx
|
||||
package clog
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package slogctx
|
||||
package clog
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue