mlog: implement LevelFromString

This commit is contained in:
Brian Picciano 2019-02-02 19:27:10 -05:00
parent 20c6f95091
commit 15b84cb173

View File

@ -22,6 +22,7 @@ import (
"os"
"sort"
"strconv"
"strings"
"sync"
"github.com/mediocregopher/mediocre-go-lib/merr"
@ -74,6 +75,26 @@ var (
FatalLevel Level = level{s: "FATAL", i: 0}
)
// LevelFromString takes a string describing one of the pre-defined Levels (e.g.
// "debug" or "INFO") and returns the corresponding Level instance, or nil if
// the string doesn't describe any of the predefined Levels.
func LevelFromString(s string) Level {
switch strings.TrimSpace(strings.ToUpper(s)) {
case "DEBUG":
return DebugLevel
case "INFO":
return InfoLevel
case "WARN":
return WarnLevel
case "ERROR":
return ErrorLevel
case "FATAL":
return FatalLevel
default:
return nil
}
}
////////////////////////////////////////////////////////////////////////////////
// KVer is used to provide context to a log entry in the form of a dynamic set