refactor: better request logging
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/deluan/navidrome/log"
|
||||||
|
"github.com/go-chi/chi/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RequestLogger(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
scheme := "http"
|
||||||
|
if r.TLS != nil {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||||
|
next.ServeHTTP(ww, r)
|
||||||
|
status := ww.Status()
|
||||||
|
|
||||||
|
message := fmt.Sprintf("HTTP: %s %s://%s%s", r.Method, scheme, r.Host, r.RequestURI)
|
||||||
|
logArgs := []interface{}{
|
||||||
|
r.Context(),
|
||||||
|
message,
|
||||||
|
"remoteAddr", r.RemoteAddr,
|
||||||
|
"lapsedTime", time.Since(start),
|
||||||
|
"httpStatus", ww.Status(),
|
||||||
|
"responseSize", ww.BytesWritten(),
|
||||||
|
}
|
||||||
|
if log.CurrentLevel() >= log.LevelDebug {
|
||||||
|
logArgs = append(logArgs, "userAgent", r.UserAgent())
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case status >= 500:
|
||||||
|
log.Error(logArgs...)
|
||||||
|
case status >= 400:
|
||||||
|
log.Warn(logArgs...)
|
||||||
|
default:
|
||||||
|
if log.CurrentLevel() >= log.LevelDebug {
|
||||||
|
log.Debug(logArgs...)
|
||||||
|
} else {
|
||||||
|
log.Info(logArgs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
+1
-1
@@ -33,7 +33,7 @@ func New(scanner *scanner.Scanner, ds model.DataStore) *Server {
|
|||||||
func (a *Server) MountRouter(path string, subRouter http.Handler) {
|
func (a *Server) MountRouter(path string, subRouter http.Handler) {
|
||||||
log.Info("Mounting routes", "path", path)
|
log.Info("Mounting routes", "path", path)
|
||||||
a.router.Group(func(r chi.Router) {
|
a.router.Group(func(r chi.Router) {
|
||||||
r.Use(middleware.Logger)
|
r.Use(RequestLogger)
|
||||||
r.Mount(path, subRouter)
|
r.Mount(path, subRouter)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/deluan/navidrome/engine"
|
"github.com/deluan/navidrome/engine"
|
||||||
|
"github.com/deluan/navidrome/log"
|
||||||
"github.com/deluan/navidrome/server/subsonic/responses"
|
"github.com/deluan/navidrome/server/subsonic/responses"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
)
|
)
|
||||||
@@ -177,5 +178,14 @@ func SendResponse(w http.ResponseWriter, r *http.Request, payload *responses.Sub
|
|||||||
w.Header().Set("Content-Type", "application/xml")
|
w.Header().Set("Content-Type", "application/xml")
|
||||||
response, _ = xml.Marshal(payload)
|
response, _ = xml.Marshal(payload)
|
||||||
}
|
}
|
||||||
|
if payload.Status == "ok" {
|
||||||
|
if log.CurrentLevel() >= log.LevelTrace {
|
||||||
|
log.Info(r.Context(), "API: Successful response", "status", "OK", "body", string(response))
|
||||||
|
} else {
|
||||||
|
log.Info(r.Context(), "API: Successful response", "status", "OK")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Warn(r.Context(), "API: Failed response", "error", payload.Error.Code, "message", payload.Error.Message)
|
||||||
|
}
|
||||||
w.Write(response)
|
w.Write(response)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ func checkRequiredParameters(next http.Handler) http.Handler {
|
|||||||
ctx = context.WithValue(ctx, "username", user)
|
ctx = context.WithValue(ctx, "username", user)
|
||||||
ctx = context.WithValue(ctx, "client", client)
|
ctx = context.WithValue(ctx, "client", client)
|
||||||
ctx = context.WithValue(ctx, "version", version)
|
ctx = context.WithValue(ctx, "version", version)
|
||||||
log.Info(ctx, "New Subsonic API request", "username", user, "client", client, "version", version, "path", r.URL.Path)
|
log.Info(ctx, "API: New request "+r.URL.Path, "username", user, "client", client, "version", version)
|
||||||
|
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|||||||
Reference in New Issue
Block a user