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
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)

View File

@ -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(),

View File

@ -1 +1 @@
1.24.1
1.24.2