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
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.Use(loggingMiddleware, recoveryMiddleware)

View File

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

View File

@ -1 +1 @@
1.23.1
1.23.2