Skip to content

Commit

Permalink
play with logger http
Browse files Browse the repository at this point in the history
  • Loading branch information
trilopin committed Sep 13, 2022
1 parent e26a8c3 commit 3abc041
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/api/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (h *GraphQLHandler) graphQL(c *Context, w http.ResponseWriter, r *http.Requ
params.OperationName,
params.Variables,
)
w.Header().Add("X-GQL-Operation", params.OperationName)

for _, err := range response.Errors {
c.logger.WithError(err).WithField("operation", params.OperationName).Error("Error executing request")
Expand Down
14 changes: 13 additions & 1 deletion server/api/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"net/http"
"time"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/sirupsen/logrus"
Expand All @@ -29,6 +30,8 @@ func LogRequest(next http.Handler) http.Handler {
recorder := statusRecorder{w, 200}
requestID := model.NewId()

startMilis := time.Now().UnixNano() / int64(time.Millisecond)

logger := logrus.WithFields(logrus.Fields{
"method": r.Method,
"url": r.URL.String(),
Expand All @@ -42,6 +45,15 @@ func LogRequest(next http.Handler) http.Handler {

next.ServeHTTP(&recorder, r)

logger.WithField("status", recorder.statusCode).Debug("Handled HTTP request")
gqlOp := recorder.Header().Get("X-GQL-Operation")
if gqlOp != "" {
logger = logger.WithField("gql_operation", gqlOp)
}

endMilis := time.Now().UnixNano() / int64(time.Millisecond)
logger.WithFields(logrus.Fields{
"time": endMilis - startMilis,
"status": recorder.statusCode,
}).Debug("Handled HTTP request")
})
}

0 comments on commit 3abc041

Please sign in to comment.