-
Notifications
You must be signed in to change notification settings - Fork 31
Allow custom implementation of toZapLevel function
#86
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
Open
nilebox
wants to merge
1
commit into
go-logr:master
Choose a base branch
from
nilebox:to-zap-level-func
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -498,6 +498,119 @@ func TestLogNumeric(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // TestCustomToZapLevel tests support for custom toZapLevel implementation. | ||
| func TestCustomToZapLevel(t *testing.T) { | ||
| type testCase struct { | ||
| name string | ||
| level zapcore.Level | ||
| toZapLevel func(lvl int) zapcore.Level | ||
| expectedLogs []string | ||
| } | ||
| customToZapLevel := func(lvl int) zapcore.Level { | ||
| if lvl > 4 { | ||
| return zapcore.DebugLevel | ||
| } | ||
| if lvl > 0 { | ||
| return zapcore.InfoLevel | ||
| } | ||
| return zapcore.WarnLevel | ||
| } | ||
|
|
||
| var testCases = []testCase{ | ||
| { | ||
| name: "default, log everything", | ||
| level: zapcore.Level(-100), // Log everything | ||
| toZapLevel: nil, // default | ||
| expectedLogs: []string{ | ||
| `{"level":"info","msg":"test 0","v":0}`, | ||
| `{"level":"debug","msg":"test 1","v":1}`, | ||
| // No matching zap level for verbosity 2 and above | ||
| `{"level":"Level(-2)","msg":"test 2","v":2}`, | ||
| `{"level":"Level(-3)","msg":"test 3","v":3}`, | ||
| `{"level":"Level(-4)","msg":"test 4","v":4}`, | ||
| `{"level":"Level(-5)","msg":"test 5","v":5}`, | ||
| `{"level":"Level(-6)","msg":"test 6","v":6}`, | ||
| `{"level":"Level(-7)","msg":"test 7","v":7}`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "default, log debug", | ||
| level: zapcore.DebugLevel, | ||
| toZapLevel: nil, // default | ||
| expectedLogs: []string{ | ||
| `{"level":"info","msg":"test 0","v":0}`, | ||
| `{"level":"debug","msg":"test 1","v":1}`, | ||
| // Logs at verbosity 2 and above are dropped | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case illustrates the main problem I'm running into, which a custom |
||
| }, | ||
| }, | ||
| { | ||
| name: "custom, log everything", | ||
| level: zapcore.Level(-100), // Log everything | ||
| toZapLevel: customToZapLevel, | ||
| expectedLogs: []string{ | ||
| `{"level":"warn","msg":"test 0","v":0}`, | ||
| `{"level":"info","msg":"test 1","v":1}`, | ||
| `{"level":"info","msg":"test 2","v":2}`, | ||
| `{"level":"info","msg":"test 3","v":3}`, | ||
| `{"level":"info","msg":"test 4","v":4}`, | ||
| `{"level":"debug","msg":"test 5","v":5}`, | ||
| `{"level":"debug","msg":"test 6","v":6}`, | ||
| `{"level":"debug","msg":"test 7","v":7}`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "custom, log info", | ||
| level: zapcore.InfoLevel, | ||
| toZapLevel: customToZapLevel, | ||
| expectedLogs: []string{ | ||
| `{"level":"warn","msg":"test 0","v":0}`, | ||
| `{"level":"info","msg":"test 1","v":1}`, | ||
| `{"level":"info","msg":"test 2","v":2}`, | ||
| `{"level":"info","msg":"test 3","v":3}`, | ||
| `{"level":"info","msg":"test 4","v":4}`, | ||
| // Logs at verbosity 5 and above are mapped to Debug and therefore dropped | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var buffer bytes.Buffer | ||
| writer := bufio.NewWriter(&buffer) | ||
| var sampleInfoLogger logr.Logger | ||
| encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{ | ||
| MessageKey: "msg", | ||
| LevelKey: "level", | ||
| EncodeLevel: zapcore.LowercaseLevelEncoder, | ||
| }) | ||
| core := zapcore.NewCore(encoder, zapcore.AddSync(writer), tc.level) | ||
| zl := zap.New(core) | ||
| opts := []zapr.Option{zapr.LogInfoLevel("v")} | ||
| if tc.toZapLevel != nil { | ||
| opts = append(opts, zapr.ToZapLevel(tc.toZapLevel)) | ||
| } | ||
| sampleInfoLogger = zapr.NewLoggerWithOptions(zl, opts...) | ||
| for i := 0; i < 8; i++ { | ||
| sampleInfoLogger.V(i).Info(fmt.Sprintf("test %d", i)) | ||
| } | ||
| if err := writer.Flush(); err != nil { | ||
| t.Fatalf("unexpected error from Flush: %v", err) | ||
| } | ||
| logOutput := buffer.String() | ||
| var lines []string | ||
| sc := bufio.NewScanner(strings.NewReader(logOutput)) | ||
| for sc.Scan() { | ||
| lines = append(lines, sc.Text()) | ||
| } | ||
| require.Equal(t, len(tc.expectedLogs), len(lines)) | ||
| for i, expected := range tc.expectedLogs { | ||
| actual := lines[i] | ||
| require.JSONEq(t, expected, actual) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestError tests Logger.Error. | ||
| func TestError(t *testing.T) { | ||
| for _, logError := range []string{"err", "error"} { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you add an example of some real-world usage of this new feature?
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 have described the real world example in #84 (comment)
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.
Also see even more detailed example below: #86 (comment)
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.
The key point that needs to be called out is that there will be several different zapr instances with the same zap backend in the same program: one for each package or set of packages with its own conventions regarding the logr level. If this is possible, then this option makes sense.
Where it breaks down is when different packages use the same logr instance (for example, by getting it from the context) and then use inconsistent log levels. That is what I was wondering about in #86 (comment).
Uh oh!
There was an error while loading. Please reload this page.
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.
Agreed, this can only be fixed by addressing the inconsistency between the original libraries.
Even then, I believe the customisable
toZapLevelfunction is a useful improvement:klog(I believe the current default implementation is still wrong to considerV(0)the only equivalent ofInfoandV(1)to be the only equivalent ofDebugandV(2)and above not even captured by standard zap level at all), i.e. allowsto be used globally instead of the default if preferred.
I also think it's better to recommend defining custom
toZapLevel(i.e. makezaprwork for the standard zap config) overzap.NewAtomicLevelAt(zapcore.Level(-2))suggested in https://github.com/go-logr/zapr?tab=readme-ov-file#increasing-verbosity (i.e. having to change the zap config itself), but it is up to you.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.
Yes, let's merge this. We just need to be sure that developers understand when and how to use it.
That depends a bit on how the zap backend gets configured. In component-base/logs we map the program's
-vparameter to thezapcore.Leveland the current mapping works as intended.Can you update the documentation as part of your PR?