Skip to content

Commit

Permalink
bugfix logger to correctly reuse logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pitabwire committed Apr 1, 2024
1 parent 9236ed2 commit 650f57c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
17 changes: 9 additions & 8 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ func Logger() Option {
if err != nil {
logLevel = logrus.InfoLevel
}

s.logger = logrus.New()
// set global log level
logrus.SetLevel(logLevel)
s.logger.SetLevel(logLevel)

logrus.SetFormatter(&logrus.TextFormatter{
s.logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
DisableQuote: true,
})
logrus.SetReportCaller(true)
logrus.SetOutput(io.Discard)
logrus.AddHook(&writer.Hook{
s.logger.SetReportCaller(true)
s.logger.SetOutput(io.Discard)
s.logger.AddHook(&writer.Hook{
Writer: os.Stderr,
LogLevels: []logrus.Level{
logrus.PanicLevel,
Expand All @@ -45,7 +47,7 @@ func Logger() Option {
logrus.WarnLevel,
},
})
logrus.AddHook(&writer.Hook{
s.logger.AddHook(&writer.Hook{
Writer: os.Stdout,
LogLevels: []logrus.Level{
logrus.InfoLevel,
Expand All @@ -54,12 +56,11 @@ func Logger() Option {
},
})

s.logLevel = &logLevel
}
}

func (s *Service) L() *logrus.Entry {
return logrus.New().WithField("service", s.Name())
return s.logger.WithField("service", s.Name())
}

func GetLoggingOptions() []logging.Option {
Expand Down
3 changes: 2 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

func TestLogs(t *testing.T) {
_, srv := frame.NewService("Logger Srv", frame.Config(
&frame.ConfigurationDefault{Oauth2WellKnownJwk: sampleWellKnownJwk}))
&frame.ConfigurationDefault{LogLevel: "Debug", Oauth2WellKnownJwk: sampleWellKnownJwk}))

logger := srv.L()
logger.Debug("testing debug logs")
logger.Info("testing logs")

err := errors.New("")
Expand Down
2 changes: 1 addition & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Service struct {
jwtClientSecret string
version string
environment string
logLevel *logrus.Level
logger *logrus.Logger
traceExporter trace.SpanExporter
traceSampler trace.Sampler
handler http.Handler
Expand Down

0 comments on commit 650f57c

Please sign in to comment.