Skip to content

Commit

Permalink
Merge pull request #42 from BugFixes/update-logger-middleware-with-lo…
Browse files Browse the repository at this point in the history
…glevel

refactor(logging): Add LogLevel filter to default logger
  • Loading branch information
Keloran authored May 13, 2024
2 parents 4cd41b1 + 8fb0cd9 commit 2126d21
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ var (
DefaultLogger func(next http.Handler) http.Handler
)

type Level int
const (
Log Level = iota

Check warning on line 25 in middleware/logger.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unused constant

Unused constant `Log`
Info
Error

Check warning on line 27 in middleware/logger.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unused constant

Unused constant `Error`
Fatal

Check warning on line 28 in middleware/logger.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unused constant

Unused constant `Fatal`
)

type LoggerSystem struct {
LogLevel Level
}

func SetupLogger(logLevel Level) *LoggerSystem {

Check warning on line 35 in middleware/logger.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unused exported function

Unused function `SetupLogger`
return &LoggerSystem{
LogLevel: logLevel,
}
}

// Logger is a middleware that logs the start and end of each request, along
// with some useful data about what was requested, what the response status was,
// and how long it took to return. When standard output is a TTY, Logger will
Expand All @@ -42,6 +60,16 @@ func Logger(next http.Handler) http.Handler {
return DefaultLogger(next)
}

func (s *LoggerSystem) Logger(next http.Handler) http.Handler {
l := RequestLogger(&DefaultLogFormatter{
Logger: log.New(os.Stdout, "", log.LstdFlags),
NoColor: false,
LogLevel: s.LogLevel,
})

return l(next)
}

// RequestLogger returns a logger handler using a custom LogFormatter.
func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
Expand All @@ -63,7 +91,7 @@ func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler {
// LogFormatter initiates the beginning of a new LogEntry per request.
// See DefaultLogFormatter for an example implementation.
type LogFormatter interface {
NewLogEntry(r *http.Request) LogEntry
NewLogEntry(r *http.Request) LogEntry
}

// LogEntry records the final log when a request completes.
Expand Down Expand Up @@ -94,6 +122,7 @@ type LoggerInterface interface {
type DefaultLogFormatter struct {
Logger LoggerInterface
NoColor bool
LogLevel Level
}

// NewLogEntry creates a new LogEntry for the request.
Expand Down Expand Up @@ -158,7 +187,9 @@ func (l *defaultLogEntry) Write(status, bytes int, elapsed time.Duration) {
cW(l.buf, l.useColor, nRed, "%s", elapsed)
}

l.Logger.Print(l.buf.String())
if l.LogLevel >= Info {
l.Logger.Print(l.buf.String())
}
}

func (l *defaultLogEntry) Panic(v interface{}) {
Expand Down

0 comments on commit 2126d21

Please sign in to comment.