Skip to content

Commit

Permalink
chore: rename FromSlog to NewCustomLogger
Browse files Browse the repository at this point in the history
This is more in line with the existing zerolog-focused API.

Also update a couple more comments, and add a test for the With method.
  • Loading branch information
mark-rushakoff committed Oct 24, 2024
1 parent c3b59df commit 8f1f86e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

The `cosmossdk.io/log` provides a zerolog logging implementation for the Cosmos SDK and Cosmos SDK modules.

To use a logger backed by an instance of the standard library's `log/slog` package, use `cosmossdk.io/log/slog`.
To use a logger wrapping an instance of the standard library's `log/slog` package, use `cosmossdk.io/log/slog`.
7 changes: 5 additions & 2 deletions log/slog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ type Logger struct {
log *slog.Logger
}

// FromSlog returns a Logger backed by an existing slog.Logger instance.
func FromSlog(log *slog.Logger) Logger {
// NewCustomLogger returns a Logger backed by an existing slog.Logger instance.
// All logging methods are called directly on the *slog.Logger;
// therefore it is the caller's responsibility to configure message filtering,
// level filtering, output format, and so on.
func NewCustomLogger(log *slog.Logger) Logger {
return Logger{log: log}
}

Expand Down
17 changes: 16 additions & 1 deletion log/slog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestSlog(t *testing.T) {
h := stdslog.NewJSONHandler(&buf, &stdslog.HandlerOptions{
Level: stdslog.LevelDebug,
})
logger := slog.FromSlog(stdslog.New(h))
logger := slog.NewCustomLogger(stdslog.New(h))

type logLine struct {
Level string `json:"level"`
Expand Down Expand Up @@ -74,4 +74,19 @@ func TestSlog(t *testing.T) {
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}

wLogger := logger.With("num", 5)
buf.Reset()
wLogger.Info("Using .With")

if err := json.Unmarshal(buf.Bytes(), &line); err != nil {
t.Fatal(err)
}
if want := (logLine{
Level: stdslog.LevelInfo.String(),
Msg: "Using .With",
Num: 5,
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}
}

0 comments on commit 8f1f86e

Please sign in to comment.