Skip to content

Commit

Permalink
fix: use the level of the record not the handler
Browse files Browse the repository at this point in the history
  • Loading branch information
telemachus committed Jul 23, 2024
2 parents b94b3bd + 1a329e0 commit c7cce65
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# humane version history

# v0.6.0

+ Fix a bug (found thanks to a user): display the level of the record not the
handler (!).

# v0.5.0

+ Switch from `exp/slog` to `log/slog` and from `exp/slices` to `slices` now
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
31 changes: 31 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
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/telemachus/humane

go 1.21
go 1.22
10 changes: 5 additions & 5 deletions humane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(" |")
}

Expand Down
2 changes: 1 addition & 1 deletion slogtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c7cce65

Please sign in to comment.