37 lines
752 B
Go
37 lines
752 B
Go
package internal
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func EnvOrExit(key string) string {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
log.Fatal().Msgf("Required env. variable '%s' is empty or missing", key)
|
|
os.Exit(1)
|
|
}
|
|
log.Debug().Msgf("Read env: %s='%s'", key, value)
|
|
return value
|
|
}
|
|
|
|
func InitLogger() {
|
|
logLevel := os.Getenv("LOG_LEVEL")
|
|
level, err := zerolog.ParseLevel(logLevel)
|
|
if err != nil {
|
|
// Default to INFO
|
|
level = zerolog.InfoLevel
|
|
}
|
|
zerolog.SetGlobalLevel(level)
|
|
|
|
if os.Getenv("ENV") == "development" {
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
} else {
|
|
log.Logger = log.Output(os.Stderr) // JSON to stdout/stderr
|
|
}
|
|
|
|
log.Debug().Msg("Logger initialized")
|
|
}
|