From d53ffd236bbbd099c5926d21bdec9487a752495c Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 6 Jun 2024 09:53:58 -0700 Subject: [PATCH] remove redundant capitalizations in unmarshalText (#1444) 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. --- zapcore/level.go | 14 +++++++------- zapcore/level_test.go | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/zapcore/level.go b/zapcore/level.go index 751cf0b98..f3e166d67 100644 --- a/zapcore/level.go +++ b/zapcore/level.go @@ -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 diff --git a/zapcore/level_test.go b/zapcore/level_test.go index eaa8e0803..6e88f7781 100644 --- a/zapcore/level_test.go +++ b/zapcore/level_test.go @@ -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)