1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: change logging middleware to use different output

This commit is contained in:
Ferdinand Mütsch 2021-02-14 16:41:02 +01:00
parent 708863fd33
commit 759e8e4dfd
3 changed files with 9 additions and 12 deletions

View File

@ -171,10 +171,7 @@ func main() {
// Globally used middlewares // Globally used middlewares
recoveryMiddleware := handlers.RecoveryHandler() recoveryMiddleware := handlers.RecoveryHandler()
loggingMiddleware := middlewares.NewLoggingMiddleware( loggingMiddleware := middlewares.NewLoggingMiddleware(logbuch.Info, []string{"/assets"})
log.New(os.Stdout, "", log.LstdFlags),
[]string{"/assets"},
)
// Router configs // Router configs
router.Use(loggingMiddleware, recoveryMiddleware) router.Use(loggingMiddleware, recoveryMiddleware)

View File

@ -4,23 +4,24 @@ package middlewares
import ( import (
"io" "io"
"log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
) )
type logFunc func(string, ...interface{})
type LoggingMiddleware struct { type LoggingMiddleware struct {
handler http.Handler handler http.Handler
output *log.Logger logFunc logFunc
excludePrefixes []string 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 func(h http.Handler) http.Handler {
return &LoggingMiddleware{ return &LoggingMiddleware{
handler: h, handler: h,
output: output, logFunc: logFunc,
excludePrefixes: excludePrefixes, excludePrefixes: excludePrefixes,
} }
} }
@ -41,9 +42,8 @@ func (lg *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
lg.output.Printf( lg.logFunc(
"%v status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s\n", "[request] status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s, user=%s\n",
time.Now().Format(time.RFC3339Nano),
ww.Status(), ww.Status(),
r.Method, r.Method,
r.URL.String(), r.URL.String(),

View File

@ -1 +1 @@
1.24.1 1.24.2