Skip to content

Commit

Permalink
feat: update InitLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Jan 13, 2025
1 parent 12e76e8 commit 4bb0b93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
7 changes: 6 additions & 1 deletion env/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ func Init() error {
if err := godotenv.Load(); err != nil {
return err
}
return InitLogger()
lvl, err := getLogLevelOrdefault()
if err != nil {
return err
}
InitLogger(lvl)
return nil
}
23 changes: 15 additions & 8 deletions env/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,33 @@ const (
)

// InitLogger setups zerolog global logger
func InitLogger() error {
lvl, err := getLogLevel()
if err != nil {
return err
}

func InitLogger(lvl zerolog.Level) {
zerolog.SetGlobalLevel(lvl)
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
return nil
}

// reads log level from env. Returns NoLevel if not specified.
func getLogLevel() (zerolog.Level, error) {
logLevelStr := os.Getenv(EnvLogLevel)
if logLevelStr == "" {
return zerolog.WarnLevel, nil
return zerolog.NoLevel, nil
}
lvl, err := zerolog.ParseLevel(logLevelStr)
if err != nil {
return lvl, fmt.Errorf("invalid LOG_LEVEL %q; %w", lvl, err)
}
return lvl, nil
}

// reads log level from env or returns a default if not specified.
func getLogLevelOrdefault() (zerolog.Level, error) {
lvl, err := getLogLevel()
if err != nil {
return lvl, err
}
if lvl == zerolog.NoLevel {
lvl = zerolog.WarnLevel
}
return lvl, nil
}

0 comments on commit 4bb0b93

Please sign in to comment.