mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 04:40:05 +01:00
feat: sdk migration in progress
This commit is contained in:
35
internal/api/mw_logger.go
Normal file
35
internal/api/mw_logger.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func zeroLogMiddleware() func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
start := time.Now()
|
||||
|
||||
next.ServeHTTP(ww, r)
|
||||
|
||||
dur := time.Since(start)
|
||||
ev := log.Info()
|
||||
if ww.Status() >= 500 {
|
||||
ev = log.Error()
|
||||
}
|
||||
ev.
|
||||
Str("remote_ip", r.RemoteAddr).
|
||||
Str("request_id", middleware.GetReqID(r.Context())).
|
||||
Str("method", r.Method).
|
||||
Str("path", r.URL.Path).
|
||||
Int("status", ww.Status()).
|
||||
Int("bytes", ww.BytesWritten()).
|
||||
Dur("duration", dur).
|
||||
Msg("http_request")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user