2021-09-29 15:38:50 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LogRequest(h fasthttp.RequestHandler, log *zap.Logger) fasthttp.RequestHandler {
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
ua = string(ctx.UserAgent())
|
|
|
|
)
|
|
|
|
|
|
|
|
h(ctx)
|
|
|
|
|
|
|
|
if strings.Contains(strings.ToLower(ua), "healthcheck") { // skip healthcheck requests logging
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("HTTP request processed",
|
|
|
|
zap.String("useragent", ua),
|
|
|
|
zap.String("method", string(ctx.Method())),
|
|
|
|
zap.String("url", string(ctx.RequestURI())),
|
2021-10-06 17:38:00 +00:00
|
|
|
zap.String("referer", string(ctx.Referer())),
|
2021-09-29 15:38:50 +00:00
|
|
|
zap.Int("status_code", ctx.Response.StatusCode()),
|
|
|
|
zap.Bool("connection_close", ctx.Response.ConnectionClose()),
|
|
|
|
zap.Duration("duration", time.Since(startedAt)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|