-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): add slog-backed Logger type (#22347)
- Loading branch information
1 parent
bf58aaf
commit 7262cf3
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Log | ||
|
||
The `cosmossdk.io/log` provides a zerolog logging implementation for the Cosmos SDK and Cosmos SDK modules. | ||
|
||
To use a logger wrapping an instance of the standard library's `log/slog` package, use `cosmossdk.io/log/slog`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Package slog contains a Logger type that satisfies [cosmossdk.io/log.Logger], | ||
// backed by a standard library [*log/slog.Logger]. | ||
package slog | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"cosmossdk.io/log" | ||
) | ||
|
||
var _ log.Logger = Logger{} | ||
|
||
// Logger satisfies [log.Logger] with logging backed by | ||
// an instance of [*slog.Logger]. | ||
type Logger struct { | ||
log *slog.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} | ||
} | ||
|
||
func (l Logger) Info(msg string, keyVals ...any) { | ||
l.log.Info(msg, keyVals...) | ||
} | ||
|
||
func (l Logger) Warn(msg string, keyVals ...any) { | ||
l.log.Warn(msg, keyVals...) | ||
} | ||
|
||
func (l Logger) Error(msg string, keyVals ...any) { | ||
l.log.Error(msg, keyVals...) | ||
} | ||
|
||
func (l Logger) Debug(msg string, keyVals ...any) { | ||
l.log.Debug(msg, keyVals...) | ||
} | ||
|
||
func (l Logger) With(keyVals ...any) log.Logger { | ||
return Logger{log: l.log.With(keyVals...)} | ||
} | ||
|
||
// Impl returns l's underlying [*slog.Logger]. | ||
func (l Logger) Impl() any { | ||
return l.log | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package slog_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
stdslog "log/slog" | ||
"testing" | ||
|
||
"cosmossdk.io/log/slog" | ||
) | ||
|
||
func TestSlog(t *testing.T) { | ||
var buf bytes.Buffer | ||
h := stdslog.NewJSONHandler(&buf, &stdslog.HandlerOptions{ | ||
Level: stdslog.LevelDebug, | ||
}) | ||
logger := slog.NewCustomLogger(stdslog.New(h)) | ||
|
||
type logLine struct { | ||
Level string `json:"level"` | ||
Msg string `json:"msg"` | ||
Num int `json:"num"` | ||
} | ||
|
||
var line logLine | ||
|
||
logger.Debug("Message one", "num", 1) | ||
if err := json.Unmarshal(buf.Bytes(), &line); err != nil { | ||
t.Fatal(err) | ||
} | ||
if want := (logLine{ | ||
Level: stdslog.LevelDebug.String(), | ||
Msg: "Message one", | ||
Num: 1, | ||
}); want != line { | ||
t.Fatalf("unexpected log record: want %v, got %v", want, line) | ||
} | ||
|
||
buf.Reset() | ||
logger.Info("Message two", "num", 2) | ||
if err := json.Unmarshal(buf.Bytes(), &line); err != nil { | ||
t.Fatal(err) | ||
} | ||
if want := (logLine{ | ||
Level: stdslog.LevelInfo.String(), | ||
Msg: "Message two", | ||
Num: 2, | ||
}); want != line { | ||
t.Fatalf("unexpected log record: want %v, got %v", want, line) | ||
} | ||
|
||
buf.Reset() | ||
logger.Warn("Message three", "num", 3) | ||
if err := json.Unmarshal(buf.Bytes(), &line); err != nil { | ||
t.Fatal(err) | ||
} | ||
if want := (logLine{ | ||
Level: stdslog.LevelWarn.String(), | ||
Msg: "Message three", | ||
Num: 3, | ||
}); want != line { | ||
t.Fatalf("unexpected log record: want %v, got %v", want, line) | ||
} | ||
|
||
buf.Reset() | ||
logger.Error("Message four", "num", 4) | ||
if err := json.Unmarshal(buf.Bytes(), &line); err != nil { | ||
t.Fatal(err) | ||
} | ||
if want := (logLine{ | ||
Level: stdslog.LevelError.String(), | ||
Msg: "Message four", | ||
Num: 4, | ||
}); 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) | ||
} | ||
} |