Skip to content

Commit

Permalink
set log level via config
Browse files Browse the repository at this point in the history
  • Loading branch information
pitabwire committed Jan 23, 2024
1 parent aee19d6 commit 5067413
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func (s *Service) Config() any {
}

type ConfigurationDefault struct {
LogLevel string `default:"info" envconfig:"LOG_LEVEL"`

ServerPort string `default:":7000" envconfig:"PORT"`
HttpServerPort string `default:":8080" envconfig:"HTTP_PORT"`
GrpcServerPort string `default:":50051" envconfig:"GRPC_PORT"`
Expand Down
35 changes: 35 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import (
"github.com/sirupsen/logrus/hooks/writer"
"io"
"os"
"strings"
)

// Logger Option that helps with initialization of our internal logger
func Logger() Option {
return func(s *Service) {

logLevel := "info"

if s.Config() != nil {
oauth2Config, ok := s.Config().(ConfigurationDefault)
if ok {
logLevel = oauth2Config.LogLevel
}
}

logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
Expand All @@ -33,6 +44,30 @@ func Logger() Option {
logrus.DebugLevel,
},
})

logLevel = strings.ToLower(logLevel)
switch logLevel {
case "debug":
logger.SetLevel(logrus.DebugLevel)
break
case "trace":
logger.SetLevel(logrus.TraceLevel)
break
case "warn":
logger.SetLevel(logrus.WarnLevel)
break
case "error":
logger.SetLevel(logrus.ErrorLevel)
break
case "fatal":
logger.SetLevel(logrus.FatalLevel)
break
default:

logger.SetLevel(logrus.InfoLevel)
break
}

s.logger = logger
}
}
Expand Down

0 comments on commit 5067413

Please sign in to comment.