diff --git a/logger.go b/logger.go index c53530c..74add03 100644 --- a/logger.go +++ b/logger.go @@ -26,7 +26,7 @@ const ( ) func init() { - InitLog(os.Getenv("LOG_LEVEL")) + InitLog(getLogLevel()) } // Logger logger interface @@ -133,3 +133,22 @@ func FatalfIf(cond bool, fmt string, args ...interface{}) { func FatalIfError(err error) { FatalfIf(err != nil, "fatal error: %v", err) } + +const logLevelDefault = "INFO" + +func isValidLevel(level string) bool { + switch strings.ToUpper(level) { + case "DEBUG", "INFO", "WARN", "ERROR": + return true + } + return false +} + +func getLogLevel() string { + level := os.Getenv("LOG_LEVEL") + if !isValidLevel(level) { + fmt.Fprintf(os.Stderr, "invalid log level: %s, switching to default: %s\n", level, logLevelDefault) + level = logLevelDefault + } + return level +}