-
Notifications
You must be signed in to change notification settings - Fork 82
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
embed funcr, custom With* function example #68
Conversation
18a0bc5
to
00db538
Compare
I don't understand what's going on with FnLogSink being a distinct type. The first commit is what I expected. I'm not clear on the second. |
Let me add a runable example. Perhaps that'll clarify this. |
00db538
to
f1226a8
Compare
I ended up refactoring funcr differently. It's now split into a formatter and the full LogSink implementation. I also addressed one aspect of PR #60 that didn't quite feel right but couldn't be solved differently at that time: adding the helper function to The downside is that more boilerplate code is needed there. The original purpose of this PR, providing a code example for a custom With* function, is served by the example stdout logger in |
650bdbf
to
d9f1bb4
Compare
t.Helper() | ||
t.Logf("%s: %s", prefix, args) | ||
l := &testlogger{ | ||
Formatter: funcr.NewFormatter(funcr.Options{}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make sense to allow configuring the formatting, in particular the log level threshold. I've not added it to the PR to not bloat it further, but would like to see this addressed before logr v1.1.0.
The simplest solution would be:
func NewTestLoggerWithOptions(t *testing.T, opts funcr.Options) logr.Logger {
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather see TestLogger get its own options struct, even if that is just a typedef to funcr.Options.
Output before:
Output now:
|
funcr/funcr.go
Outdated
@@ -95,22 +88,46 @@ const ( | |||
|
|||
const timestampFmt = "2006-01-02 15:04:05.000000" | |||
|
|||
// fnlogger inherits most of its LogSink implementation from FnLogSink | |||
// and just needs to adapt the return value of the With functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment needs to be updated...
funcr/example_test.go
Outdated
@@ -34,3 +34,77 @@ func ExampleUnderlier() { | |||
} | |||
// Output: hello world | |||
} | |||
|
|||
// newStdoutLogger returns a logr.Logger that prints to stdout. | |||
// It demonstrates how to implement a custom With* method which |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/method/function
d9f1bb4
to
89e8c23
Compare
This allows custom loggers to provide their own additional methods while using most of the implementation from funcr. That funcr had to be extended to support the helper concept in testing.T didn't feel right because fnlogger then always implemented the CallStackHelperLogSink interface even when the helper was nil. This gets fixed by moving the implementation of that into the testing logger, using the new embedding mechanism of funcr. As a drive-by fix, the testing logger no longer prints a redundant ":" when the prefix is empty. The logr package describes how to implement a custom With* function. A runable example for that gets added in funcr/example_test.go.
89e8c23
to
8836100
Compare
Thanks! /lgtm |
This allows custom loggers to provide their own additional methods
while using most of the implementation from funcr.
That funcr had to be extended to support the helper concept in
testing.T didn't feel right because fnlogger then always implemented
the CallStackHelperLogSink interface even when the helper was
nil. This gets fixed by moving the implementation of that into the
testing logger, using the new embedding mechanism of funcr.
As a drive-by fix, the testing logger no longer prints a redundant ":"
when the prefix is empty.
The logr package describes how to implement a custom With* function. A
runable example for that gets added in funcr/example_test.go.