Skip to content

Commit

Permalink
feat: log fatal requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangwanpeng committed Jul 19, 2023
1 parent 12b7f1d commit f3d803a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/service/accesslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package service

import (
"bytes"
"io"
"net/http"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -44,5 +46,21 @@ func AccessLog() gin.HandlerFunc {
zap.Int64("cost", end-start),
zap.String("user-agent", c.Request.Header.Get("User-Agent")),
)

if bodyWriter.ResponseWriter.Status() != http.StatusOK {
body, _ := io.ReadAll(bodyWriter.body)
logger.Error(
"error processing request",
zap.String("remote", c.RemoteIP()),
zap.String("method", c.Request.Method),
zap.String("url", c.Request.RequestURI),
zap.String("proto", c.Request.Proto),
zap.Int("status", bodyWriter.ResponseWriter.Status()),
zap.Int("length", bodyWriter.body.Len()),
zap.Int64("cost", end-start),
zap.String("user-agent", c.Request.Header.Get("User-Agent")),
zap.String("result", string(body)),
)
}
}
}

0 comments on commit f3d803a

Please sign in to comment.