Skip to content

Commit

Permalink
Add request_id in log
Browse files Browse the repository at this point in the history
  • Loading branch information
dynastymasra committed Mar 12, 2020
1 parent 7b0915f commit 8df16e2
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions negroni/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"
"time"

"github.com/dynastymasra/cookbook"

"github.com/sirupsen/logrus"

"github.com/urfave/negroni"
Expand All @@ -12,21 +14,23 @@ import (
func LogrusLog(name string) negroni.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
startTime := time.Now().UTC()
requestID := r.Header.Get(cookbook.XRequestID)

next(w, r)

responseTime := time.Now().UTC()
deltaTime := responseTime.Sub(startTime)

logrus.WithFields(logrus.Fields{
"request_time": startTime.Format(time.RFC3339),
"delta_time": deltaTime,
"response_time": responseTime.Format(time.RFC3339),
"request_proxy": r.RemoteAddr,
"url": r.URL.Path,
"method": r.Method,
"request_source": r.Header.Get("X-FORWARDED-FOR"),
"headers": r.Header,
"request_time": startTime.Format(time.RFC3339),
"delta_time": deltaTime,
"response_time": responseTime.Format(time.RFC3339),
"request_proxy": r.RemoteAddr,
"url": r.URL.Path,
"method": r.Method,
"request_source": r.Header.Get("X-FORWARDED-FOR"),
"headers": r.Header,
cookbook.RequestID: requestID,
}).Infoln("HTTP Request", name)
}
}

0 comments on commit 8df16e2

Please sign in to comment.