Skip to content

Commit

Permalink
Merge pull request #5 from eltorocorp/AddRequestDataToOutGoingLog
Browse files Browse the repository at this point in the history
Put the server interceptor before the  interceptor to have the reques…
  • Loading branch information
dsegundo2 authored Apr 26, 2022
2 parents c6f3553 + 4bc443d commit c779e94
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions xrequestid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ xrequestid is an grpc interceptor which receives request id from metadata and se
- Dependencies Added:
- github.com/sirupsen/logrus
- github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus
- Put the server interceptor before the `github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus` interceptor to have the request ID and data included in the response log

## Usage

Expand Down
18 changes: 12 additions & 6 deletions xrequestid/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ func defaultReqeustIDValidator(requestID string) bool {

// Adds the incoming request & ID to the logrus entry in context
func addRequestToLogger(ctx context.Context, requestID string, requestData interface{}) context.Context {
log := ctxlogrus.Extract(ctx).WithFields(
logrus.Fields{
"Request ID": requestID,
"Request Data": fmt.Sprintf("%+v", requestData),
})
// Create a new logger if the logrus logger is in panic level (most likeyly a nullLogger)
log := logrus.NewEntry(logrus.New())
if l := ctxlogrus.Extract(ctx); l.Level != logrus.PanicLevel {
log = l
}

log.Log(log.Logger.GetLevel(), "RequestID added")
// Attach Request ID and Data
log = log.WithFields(logrus.Fields{
"Request ID": requestID,
"Request Data": fmt.Sprintf("%+v", requestData),
})

log.Info("Request ID added")
return ctxlogrus.ToContext(ctx, log)
}

0 comments on commit c779e94

Please sign in to comment.