Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log.ToLogger and log.ToSlogger helper functions #3629

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"time"

"github.com/TheZeroSlave/zapsentry"
Expand Down Expand Up @@ -184,6 +186,16 @@ func WithGlobalRedaction() func(*sinkConfig) {
}
}

// ToLogger converts the logr.Logger into a legacy *log.Logger.
func ToLogger(l logr.Logger) *log.Logger {
return slog.NewLogLogger(logr.ToSlogHandler(l), slog.LevelInfo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I might be missing something or there are too many logs/slogs combos 🤣 , but should this be log. or should the comment be updated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It compiles so it's correct, lol! It's doing logr.Logger -> slog.Handler -> *log.Logger.

The slog package has a function to "bridge" slog to the old log lib. See slog.NewLogLogger.

The logger acts as a bridge from the older log API to newer structured logging handlers.

}

// ToSlogger converts the logr.Logger into a *slog.Logger.
func ToSlogger(l logr.Logger) *slog.Logger {
return slog.New(logr.ToSlogHandler(l))
}

// firstErrorFunc is a helper function that returns a function that executes
// all provided args and returns the first error, if any.
func firstErrorFunc(fs ...func() error) func() error {
Expand Down
46 changes: 46 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,52 @@ func TestGlobalRedaction_JSON(t *testing.T) {
)
}

func TestToLogger(t *testing.T) {
var jsonBuffer bytes.Buffer
l, flush := New("service-name",
WithJSONSink(&jsonBuffer),
)
logger := ToLogger(l)
logger.Println("yay")
assert.Nil(t, flush())

var parsedJSON map[string]any
assert.Nil(t, json.Unmarshal(jsonBuffer.Bytes(), &parsedJSON))
assert.NotEmpty(t, parsedJSON["ts"])
delete(parsedJSON, "ts")
delete(parsedJSON, "caller") // log.Logger adds a "caller" field
assert.Equal(t,
map[string]any{
"level": "info-0",
"logger": "service-name",
"msg": "yay",
},
parsedJSON,
)
}

func TestToSlogger(t *testing.T) {
var jsonBuffer bytes.Buffer
l, flush := New("service-name", WithJSONSink(&jsonBuffer))
logger := ToSlogger(l)
logger.Info("yay")
assert.Nil(t, flush())

var parsedJSON map[string]any
assert.Nil(t, json.Unmarshal(jsonBuffer.Bytes(), &parsedJSON))
assert.NotEmpty(t, parsedJSON["ts"])
delete(parsedJSON, "ts")
delete(parsedJSON, "caller") // slog.Logger adds a "caller" field
assert.Equal(t,
map[string]any{
"level": "info-0",
"logger": "service-name",
"msg": "yay",
},
parsedJSON,
)
}

func BenchmarkLoggerRedact(b *testing.B) {
msg := "this is a message with 'foo' in it"
logKvps := []any{"key", "value", "foo", "bar", "bar", "baz", "longval", "84hblnqwp97ewilbgoab8fhqlngahs6dl3i269haa"}
Expand Down
Loading