Skip to content

Commit

Permalink
Change to use ULID as request ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeQ committed Jun 6, 2017
1 parent 23d693f commit 8af3fe8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
14 changes: 7 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package: github.com/drkaka/lg
import:
- package: github.com/satori/go.uuid
version: ^1.1.0
- package: github.com/drkaka/ulid
- package: go.uber.org/zap
version: ^1.0.0
subpackages:
Expand Down
12 changes: 6 additions & 6 deletions lg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"net/http"
"time"

"github.com/drkaka/ulid"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

uuid "github.com/satori/go.uuid"
)

var (
Expand Down Expand Up @@ -60,16 +59,17 @@ func L(ctx context.Context) *zap.Logger {
// LogRequest to log every request.
func LogRequest(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// Start timer
start := time.Now()

// Add a requestID field to logger
l := log.With(zap.String("requestID", uuid.NewV1().String()))
uid, _ := ulid.NewFromTime(start)
l := log.With(zap.String("requestID", uid.String()))
// Add logger to context
ctx := context.WithValue(r.Context(), requestIDKey, l)
// Request with this new context.
r = r.WithContext(ctx)

// Start timer
start := time.Now()

// wrap the ResponseWriter
lw := &basicWriter{ResponseWriter: w}

Expand Down

0 comments on commit 8af3fe8

Please sign in to comment.