From 15b84cb1731fdd3c87c721fd300f8f9fc8d65aa7 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 2 Feb 2019 19:27:10 -0500 Subject: [PATCH] mlog: implement LevelFromString --- mlog/mlog.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mlog/mlog.go b/mlog/mlog.go index 9570ded..54caf22 100644 --- a/mlog/mlog.go +++ b/mlog/mlog.go @@ -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