Skip to content

Commit

Permalink
remove redundant capitalizations in unmarshalText (#1444)
Browse files Browse the repository at this point in the history
unmarshalText currently checks for almost all of the levels as
the configured string values as well as their capitalized versions.

But we call unmarshalText with bytes.ToLower() when we actually parse
the levels, so we do not need to check for the capitalized versions
separately since this is redundant.

Previous version of this PR was trying to add "WARNING" as a check
to this since that was recently added but the corresponding capitalized
string wasn't checked, but @abhinav pointed out that we actually call
this with ToLower, so it's better to remove the redundant checks.
  • Loading branch information
sywhang committed Jun 6, 2024
1 parent 2da446f commit d53ffd2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
14 changes: 7 additions & 7 deletions zapcore/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,19 @@ func (l *Level) UnmarshalText(text []byte) error {

func (l *Level) unmarshalText(text []byte) bool {
switch string(text) {
case "debug", "DEBUG":
case "debug":
*l = DebugLevel
case "info", "INFO", "": // make the zero value useful
case "info", "": // make the zero value useful
*l = InfoLevel
case "warn", "warning", "WARN":
case "warn", "warning":
*l = WarnLevel
case "error", "ERROR":
case "error":
*l = ErrorLevel
case "dpanic", "DPANIC":
case "dpanic":
*l = DPanicLevel
case "panic", "PANIC":
case "panic":
*l = PanicLevel
case "fatal", "FATAL":
case "fatal":
*l = FatalLevel
default:
return false
Expand Down
1 change: 1 addition & 0 deletions zapcore/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestParseLevel(t *testing.T) {
{"info", InfoLevel, ""},
{"DEBUG", DebugLevel, ""},
{"FOO", 0, `unrecognized level: "FOO"`},
{"WARNING", WarnLevel, ""},
}
for _, tt := range tests {
parsedLevel, err := ParseLevel(tt.text)
Expand Down

0 comments on commit d53ffd2

Please sign in to comment.