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:
parent
b1c1f14e35
commit
b0480356de
5
main.go
5
main.go
@ -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)
|
||||
|
@ -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),
|
||||
|
@ -1 +1 @@
|
||||
1.23.1
|
||||
1.23.2
|
Loading…
Reference in New Issue
Block a user