Skip to content

Commit

Permalink
rename to clog (#6)
Browse files Browse the repository at this point in the history
* rename to clog

Signed-off-by: Jason Hall <[email protected]>

* fix example in README

Signed-off-by: Jason Hall <[email protected]>

---------

Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh authored Jan 16, 2024
1 parent c071a43 commit 04bee69
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 44 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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")
}
```

Expand All @@ -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")
})
}
}
Expand All @@ -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
Expand All @@ -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")
}
```

Expand Down
18 changes: 9 additions & 9 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
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:
// level=INFO msg="hello world" baz=true foo=bar
}

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
Expand Down
8 changes: 4 additions & 4 deletions examples/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
10 changes: 5 additions & 5 deletions examples/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
6 changes: 3 additions & 3 deletions examples/logger/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ 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) {
ctx := slogtest.TestContextWithLogger(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 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/chainguard-dev/slogctx
module github.com/chainguard-dev/clog

go 1.21.2
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slogctx
package clog

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slogctx
package clog

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slogctx
package clog

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slogctx
package clog

import (
"context"
Expand Down
6 changes: 3 additions & 3 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slogctx
package clog

import (
"bytes"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions slogtest/slogtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"log/slog"

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

var (
Expand All @@ -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.
Expand Down

0 comments on commit 04bee69

Please sign in to comment.