Skip to content
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

Merged
merged 5 commits into from Jan 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion zapcore/level.go
Expand Up @@ -26,7 +26,10 @@ import (
"fmt"
)

var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level")
var (
errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level")
errInvalidLevel = errors.New("invalid level")
)

// A Level is a logging priority. Higher levels are more important.
type Level int8
Expand Down Expand Up @@ -55,6 +58,32 @@ const (
_maxLevel = FatalLevel
)

// LevelFromString returns a level based on the lower-case or all-caps ASCII
// representation of the log level.
//
// This is particularly useful when dealing with text input to configure log
// levels.
func LevelFromString(text string) (Level, error) {
switch text {
case "debug", "DEBUG":
return DebugLevel, nil
case "info", "INFO", "": // make the zero value useful
return InfoLevel, nil
case "warn", "WARN":
return WarnLevel, nil
case "error", "ERROR":
return ErrorLevel, nil
case "dpanic", "DPANIC":
return DPanicLevel, nil
case "panic", "PANIC":
return PanicLevel, nil
case "fatal", "FATAL":
return FatalLevel, nil
default:
return -2, errInvalidLevel
}
}

// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
switch l {
Expand Down