Skip to content

Commit

Permalink
added NewGlogrusWithRedId to support request id's from other middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoklovic committed Mar 1, 2016
1 parent cee5818 commit 69a9fbb
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions glogrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ func (glogr glogrus) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
// }
//
func NewGlogrus(l *logrus.Logger, name string) func(goji.Handler) goji.Handler {
return NewGlogrusWithReqId(l, name, emptyRequestId)
}

func NewGlogrusWithReqId(l *logrus.Logger, name string, reqidf func(context.Context) string) func(goji.Handler) goji.Handler {
return func(h goji.Handler) goji.Handler {
fn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
start := time.Now()

//TODO: figure out how to get a proper reqId in the context
//reqID := middleware.GetReqID(*glogr.c)
reqID := reqidf(ctx)

l.WithFields(logrus.Fields{
//"req_id": reqID,
"req_id": reqID,
"uri": r.RequestURI,
"method": r.Method,
"remote": r.RemoteAddr,
Expand All @@ -96,7 +99,7 @@ func NewGlogrus(l *logrus.Logger, name string) func(goji.Handler) goji.Handler {
latency := float64(time.Since(start)) / float64(time.Millisecond)

l.WithFields(logrus.Fields{
//"req_id": reqID,
"req_id": reqID,
"status": lresp.status(),
"method": r.Method,
"uri": r.RequestURI,
Expand All @@ -109,3 +112,7 @@ func NewGlogrus(l *logrus.Logger, name string) func(goji.Handler) goji.Handler {
}

}

func emptyRequestId(ctx context.Context) string {
return ""
}

0 comments on commit 69a9fbb

Please sign in to comment.