Skip to content

Commit

Permalink
fix slog WithAttrs
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Apr 27, 2024
1 parent 50d8eae commit b03e4f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion logger_go1.21.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (h *slogHandler) Handle(_ context.Context, r slog.Record) error {
}

func (h *slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
e := NewContext(nil)
e := NewContext(h.Logger.Context)
for _, attr := range attrs {
e = e.Any(attr.Key, attr.Value)
}
Expand Down
19 changes: 19 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
stdLog "log"
"log/slog"
"net"
"os"
"strings"
Expand Down Expand Up @@ -540,6 +541,24 @@ func TestStdLogger(t *testing.T) {
stdLog.Printf("hello from stdLog %s", "Printf")
}

func TestSlog(t *testing.T) {
var logger *slog.Logger = (&Logger{
Level: InfoLevel,
TimeField: "date",
TimeFormat: "2006-01-02",
Caller: 1,
}).Slog()

sublogger := logger.With("logger", "a_test_slog").With("everything", 42)
sublogger.Info("hello from sub slog Info")
sublogger.Warn("hello from sub slog Warn")
sublogger.Error("hello from sub slog Error")

logger.Info("hello from slog Info")
logger.Warn("hello from slog Warn")
logger.Error("hello from slog Error")
}

type errno uint

func (e errno) Error() string {
Expand Down

0 comments on commit b03e4f5

Please sign in to comment.