Skip to content

Commit

Permalink
Merge pull request #1 from ostafen/fix/init
Browse files Browse the repository at this point in the history
Use default value instead of panicking when level is invalid
  • Loading branch information
yedf2 authored Dec 1, 2022
2 parents ac1eb03 + 9e849e0 commit 320298e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
)

func init() {
InitLog(os.Getenv("LOG_LEVEL"))
InitLog(getLogLevel())
}

// Logger logger interface
Expand Down Expand Up @@ -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
}

0 comments on commit 320298e

Please sign in to comment.