-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
zapcore: Add ParseLevel #1047
zapcore: Add ParseLevel #1047
Conversation
This adds the public LevelFromString func which enables the user to construct a log level based on ASCII text input
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.
Hey there! Thanks for the contribution.
I'm not opposed to a convenience function, but I think:
- It should be named UnmarshalLevel to clearly state that we're using the unmarshaling logic, which brings me to
- It should not re-implement the unmarshaling logic. It should use the UnmarshalText method
Makes sense. I already thought about that 👍 I will rework the implementation and will update this PR. |
I adjusted the implementation. Adjusted Usagelevel, err := zapcore.UnmarshalLevel("info") // zapcore.InfoLevel, nil
level, err := zapcore.UnmarshalLevel("DEBUG") // zapcore.DebugLevel, nil
level, err := zapcore.UnmarshalLevel("FOO") // zapcore.DebugLevel, "unrecognized level: FOO" I was also thinking about adding a |
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.
Could you please add a couple test cases for the new function in level_test.go? The cases you demonstrated in the comments should suffice.
RE: AtomicLevelFromString: Seems like it might be unnecessary since you can just call that on the result of UnmarshalLevel?
Codecov Report
@@ Coverage Diff @@
## master #1047 +/- ##
=======================================
Coverage 98.21% 98.21%
=======================================
Files 48 48
Lines 2071 2074 +3
=======================================
+ Hits 2034 2037 +3
Misses 29 29
Partials 8 8
Continue to review full report at Codecov.
|
Oh, re: this:
The contract for |
I added the tests. RE: AtomicLevelFromString: This seems like a similar convenience function as level := zap.NewAtomicLevel()
err := level.UnmarshalText(byte("debug")) We could condense this into: // Or zap.NewAtomicLevelFrom()
level := zap.NewAtomicLevelFromString("debug") // level = DebugLevel RE this:
Yeah this is usually how I handle this as well. But usually I set the first return argument to something which cleary states an error occurred. Returning |
- We can use backticks to avoid having to escape quotes - Prefer to have the error and non-error paths branch so that we're not asserting the value of level in the error case (since that's specifically not part of the contract)
Fair enough. If you have need for it, a zap.ParseAtomicLevel would be fine. |
Great! I will add this asap. PS: Thanks for the great input on the tests as this is the first time I work with tests in Go. |
Thanks! |
As discussed in #1047 this PR adds a zap.ParseAtomicLevel() func which enables the user to construct a log level based on ASCII text input. Usage ``` level, err := zap.ParseAtomicLevel("info") // InfoLevel, nil level, err := zap.ParseAtomicLevel("DEBUG") // DebugLevel, nil level, err := zap.ParseAtomicLevel("FOO") // InfoLevel, "unrecognized level: "FOO"" ``` Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com> Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
This adds the public ParseLevel function which enables the user to
construct a log level based on ASCII text input.
This re-uses the UnmarshalText method of Level for the heavy-lifting,
but it avoids the user having to declare a local
Level
variable first.Usage
One thing I'm not quite sure is the returned Level value of
-2
when the user provided a invalid log level as text. Usually I would go for-1
but this is already used byzapcore.DebugLevel
. Let me know if you know any better solution.