Add ParseLogLevel() function

This commit is contained in:
Elara 2023-01-04 22:40:16 +00:00
parent 8aeea81afa
commit d5add47d25

17
main.go
View File

@ -2,6 +2,8 @@ package logger
import ( import (
"fmt" "fmt"
"errors"
"strings"
) )
// LogLevel represents a log level // LogLevel represents a log level
@ -26,6 +28,21 @@ var logLevelNames = [...]string{
LogLevelPanic: "panic", LogLevelPanic: "panic",
} }
// ErrNoSuchLevel is returned when ParseLogLevel cannot find
// a log level corresponding to the provided string
var ErrNoSuchLevel = errors.New("no such log level")
// ParseLogLevel parses a string representing a log level
// and returns the proper log level
func ParseLogLevel(s string) (LogLevel, error) {
for lvl, name := range logLevelNames {
if strings.EqualFold(name, s) {
return LogLevel(lvl), nil
}
}
return 254, ErrNoSuchLevel
}
// Logger represents a logger // Logger represents a logger
type Logger interface { type Logger interface {
// NoPanic prevents the logger from panicking on panic events // NoPanic prevents the logger from panicking on panic events