From 1a329e012e5c075ffe0f0c620c4d6625d7fe448e Mon Sep 17 00:00:00 2001 From: Peter Aronoff Date: Tue, 23 Jul 2024 08:35:51 -0400 Subject: [PATCH] fix: write the level of the record not the handler --- .github/workflows/go.yml | 6 +++--- docs/README.md | 2 +- example_test.go | 31 +++++++++++++++++++++++++++++++ go.mod | 2 +- humane.go | 10 +++++----- slogtest_test.go | 2 +- 6 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 example_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 55c35b5..ee08dcf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -13,11 +13,11 @@ jobs: - ubuntu-latest - macos-latest - windows-latest - go: ['1.21'] + go: ['1.21', '1.22'] name: humane test (using go ${{ matrix.go }} on ${{ matrix.os }}) steps: - - uses: actions/checkout@v3 - - uses: actions/setup-go@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - run: make test diff --git a/docs/README.md b/docs/README.md index f3af36f..6ff3bf6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -51,7 +51,7 @@ logger.Error("Ooops", slog.Any("error", err)) // You can also set options. Again, see the next section for more details. opts := &humane.Options{ Level: slog.LevelError, - TimeFormat: time.RFC3339 + TimeFormat: time.RFC3339, } logger := slog.New(humane.NewHandler(os.Stderr, opts)) logger.Info("This message will not be written") diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..f4858f0 --- /dev/null +++ b/example_test.go @@ -0,0 +1,31 @@ +package humane_test + +import ( + "fmt" + "log/slog" + "os" + + "github.com/telemachus/humane" +) + +func ExampleInfo() { + opts := &humane.Options{ReplaceAttr: removeTime, Level: slog.LevelDebug} + logger := slog.New(humane.NewHandler(os.Stdout, opts)) + logger.Debug("Foo") + logger.Info("Bar") + logger.Warn("Fizz") + logger.Error("Buzz") + logger.Error("Error", slog.Any("error", fmt.Errorf("xxxx"))) + logger.Warn("Warn", "foo", "bar") + logger.Info("Info", "fizz", "buzz") + logger.Debug("Debug", "status", "hello, world") + // Output: + // DEBUG | Foo | + // INFO | Bar | + // WARN | Fizz | + // ERROR | Buzz | + // ERROR | Error | error=xxxx + // WARN | Warn | foo=bar + // INFO | Info | fizz=buzz + // DEBUG | Debug | status="hello, world" +} diff --git a/go.mod b/go.mod index fd62b0b..c9e20c5 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/telemachus/humane -go 1.21 +go 1.22 diff --git a/humane.go b/humane.go index e941ab4..080850e 100644 --- a/humane.go +++ b/humane.go @@ -104,11 +104,11 @@ func (h *handler) Enabled(_ context.Context, l slog.Level) bool { func (h *handler) Handle(_ context.Context, r slog.Record) error { buf := buffer.New() defer buf.Free() - h.appendLevel(buf) + h.appendLevel(buf, r.Level) buf.WriteByte(' ') buf.WriteString(r.Message) buf.WriteString(" |") - if len(h.attrs) > 0 { + if h.attrs != "" { buf.WriteString(h.attrs) } r.Attrs(func(a slog.Attr) bool { @@ -174,13 +174,13 @@ func (h *handler) clone() *handler { } } -func (h *handler) appendLevel(buf *buffer.Buffer) { - if lVal, ok := levelValues[h.level.Level()]; ok { +func (h *handler) appendLevel(buf *buffer.Buffer, level slog.Level) { + if lVal, ok := levelValues[level.Level()]; ok { buf.WriteString(lVal) return } buf.WriteByte(' ') - buf.WriteString(h.level.Level().String()) + buf.WriteString(level.Level().String()) buf.WriteString(" |") } diff --git a/slogtest_test.go b/slogtest_test.go index 9eeebd6..83d14c7 100644 --- a/slogtest_test.go +++ b/slogtest_test.go @@ -51,7 +51,7 @@ func parseHumane(bs []byte) (map[string]any, error) { // that contains a space. For this test, however, this will work---as // long as I make sure to set a time format without whitespace. s = pieces[2] - for len(s) > 0 { + for s != "" { kv, rest, _ := strings.Cut(s, " ") k, value, found := strings.Cut(kv, "=") if !found {