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

chore: exclude static assets from request logging

This commit is contained in:
Ferdinand Mütsch 2021-02-07 12:50:02 +01:00
parent b1c1f14e35
commit b0480356de
3 changed files with 20 additions and 7 deletions

View File

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

View File

@ -6,19 +6,22 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"strings"
"time" "time"
) )
type LoggingMiddleware struct { type LoggingMiddleware struct {
handler http.Handler handler http.Handler
output *log.Logger output *log.Logger
excludePrefixes []string
} }
func NewLoggingMiddleware(output *log.Logger) func(http.Handler) http.Handler { func NewLoggingMiddleware(output *log.Logger, 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, output: output,
excludePrefixes: excludePrefixes,
} }
} }
} }
@ -31,6 +34,13 @@ func (lg *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
end := time.Now() end := time.Now()
duration := end.Sub(start) duration := end.Sub(start)
path := strings.ToLower(r.URL.Path)
for _, prefix := range lg.excludePrefixes {
if strings.HasPrefix(path, prefix) {
return
}
}
lg.output.Printf( lg.output.Printf(
"%v status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s\n", "%v status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s\n",
time.Now().Format(time.RFC3339Nano), time.Now().Format(time.RFC3339Nano),

View File

@ -1 +1 @@
1.23.1 1.23.2