From 759e8e4dfdba92de14d996f711f9b4df40722278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 14 Feb 2021 16:41:02 +0100 Subject: [PATCH] chore: change logging middleware to use different output --- main.go | 5 +---- middlewares/logging.go | 14 +++++++------- version.txt | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index bc51cc1..9c64059 100644 --- a/main.go +++ b/main.go @@ -171,10 +171,7 @@ func main() { // Globally used middlewares recoveryMiddleware := handlers.RecoveryHandler() - loggingMiddleware := middlewares.NewLoggingMiddleware( - log.New(os.Stdout, "", log.LstdFlags), - []string{"/assets"}, - ) + loggingMiddleware := middlewares.NewLoggingMiddleware(logbuch.Info, []string{"/assets"}) // Router configs router.Use(loggingMiddleware, recoveryMiddleware) diff --git a/middlewares/logging.go b/middlewares/logging.go index 08440c2..65fcd3d 100644 --- a/middlewares/logging.go +++ b/middlewares/logging.go @@ -4,23 +4,24 @@ package middlewares import ( "io" - "log" "net/http" "strings" "time" ) +type logFunc func(string, ...interface{}) + type LoggingMiddleware struct { handler http.Handler - output *log.Logger + logFunc logFunc excludePrefixes []string } -func NewLoggingMiddleware(output *log.Logger, excludePrefixes []string) func(http.Handler) http.Handler { +func NewLoggingMiddleware(logFunc logFunc, excludePrefixes []string) func(http.Handler) http.Handler { return func(h http.Handler) http.Handler { return &LoggingMiddleware{ handler: h, - output: output, + logFunc: logFunc, excludePrefixes: excludePrefixes, } } @@ -41,9 +42,8 @@ func (lg *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - lg.output.Printf( - "%v status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s\n", - time.Now().Format(time.RFC3339Nano), + lg.logFunc( + "[request] status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s, user=%s\n", ww.Status(), r.Method, r.URL.String(), diff --git a/version.txt b/version.txt index 3e940eb..0b4c970 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.24.1 \ No newline at end of file +1.24.2 \ No newline at end of file