Merge branch 'main' of git.mstar.dev:mstar/goutils
This commit is contained in:
commit
51b35036ab
1 changed files with 47 additions and 6 deletions
|
@ -5,19 +5,36 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func BuildLoggingMiddleware(extras map[string]string) HandlerBuilder {
|
||||
func BuildLoggingMiddleware(
|
||||
status500IsError bool,
|
||||
ignorePaths []string,
|
||||
extras map[string]string,
|
||||
) HandlerBuilder {
|
||||
return func(h http.Handler) http.Handler {
|
||||
return ChainMiddlewares(h,
|
||||
hlog.NewHandler(log.Logger),
|
||||
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||
if strings.HasPrefix(r.URL.Path, "/assets") {
|
||||
for _, p := range ignorePaths {
|
||||
if strings.HasPrefix(r.URL.Path, p) {
|
||||
return
|
||||
}
|
||||
logger := hlog.FromRequest(r).Info().
|
||||
}
|
||||
var logger *zerolog.Event
|
||||
if status >= 500 {
|
||||
if status500IsError {
|
||||
logger = hlog.FromRequest(r).Error()
|
||||
} else {
|
||||
logger = hlog.FromRequest(r).Warn()
|
||||
}
|
||||
} else {
|
||||
logger = hlog.FromRequest(r).Info()
|
||||
}
|
||||
logger = logger.
|
||||
Str("method", r.Method).
|
||||
Stringer("url", r.URL).
|
||||
Int("status", status).
|
||||
|
@ -28,7 +45,7 @@ func BuildLoggingMiddleware(extras map[string]string) HandlerBuilder {
|
|||
}
|
||||
logger.Send()
|
||||
}),
|
||||
hlog.RemoteAddrHandler("ip"),
|
||||
RealIpAppenderMiddleware("ip"),
|
||||
hlog.UserAgentHandler("user_agent"),
|
||||
hlog.RefererHandler("referer"),
|
||||
hlog.RequestIDHandler("req_id", "Request-Id"),
|
||||
|
@ -51,9 +68,33 @@ func LoggingMiddleware(handler http.Handler) http.Handler {
|
|||
Dur("duration", duration).
|
||||
Send()
|
||||
}),
|
||||
hlog.RemoteAddrHandler("ip"),
|
||||
RealIpAppenderMiddleware("ip"),
|
||||
hlog.UserAgentHandler("user_agent"),
|
||||
hlog.RefererHandler("referer"),
|
||||
hlog.RequestIDHandler("req_id", "Request-Id"),
|
||||
)
|
||||
}
|
||||
|
||||
// hlog.RemoteAddrHandler except fixed to check the X-Real-Ip and X-Forwarded-For
|
||||
// headers first for the IP instead of relying on RemoteAddr
|
||||
// (which would only return the last proxy's address instead of the caller's)
|
||||
func RealIpAppenderMiddleware(fieldKey string) func(handler http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
IPAddress := r.Header.Get("X-Real-Ip")
|
||||
if IPAddress == "" {
|
||||
IPAddress = r.Header.Get("X-Forwarded-For")
|
||||
}
|
||||
if IPAddress == "" {
|
||||
IPAddress = r.RemoteAddr
|
||||
}
|
||||
if IPAddress != "" {
|
||||
log := zerolog.Ctx(r.Context())
|
||||
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||
return c.Str(fieldKey, IPAddress)
|
||||
})
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue