diff --git a/config.go b/config.go index 53a660e..6d798c7 100644 --- a/config.go +++ b/config.go @@ -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"` diff --git a/logger.go b/logger.go index f5eb085..8d2ca90 100644 --- a/logger.go +++ b/logger.go @@ -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, @@ -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 } }