-
Notifications
You must be signed in to change notification settings - Fork 6
/
glogrus.go
108 lines (98 loc) · 2.78 KB
/
glogrus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package glogrus
import (
"fmt"
"net/http"
"time"
"github.com/Sirupsen/logrus"
"goji.io"
"golang.org/x/net/context"
)
// NewGlogrus allows you to configure a goji middleware that logs all requests and responses
// using the structured logger logrus. It takes the logrus instance and the name of the app
// as the parameters and returns a middleware of type "func(goji.Handler) goji.Handler"
//
// Example:
//
// package main
//
// import(
// ""goji.io"
// "github.com/goji/glogrus2"
// "github.com/Sirupsen/logrus"
// )
//
// func main() {
//
// logr := logrus.New()
// logr.Formatter = new(logrus.JSONFormatter)
// goji.Use(glogrus.NewGlogrus(logr, "my-app-name"))
//
// goji.Get("/ping", yourHandler)
// goji.Serve()
// }
//
func NewGlogrus(l *logrus.Logger, name string) func(goji.Handler) goji.Handler {
return NewGlogrusWithReqId(l, name, emptyRequestId)
}
// NewGlogrusWithReqId allows you to configure a goji middleware that logs all requests and responses
// using the structured logger logrus. It takes the logrus instance, the name of the app and a function
// that can retrieve a requestId from the Context "func(context.Context) string"
// as the parameters and returns a middleware of type "func(goji.Handler) goji.Handler"
//
// Passing in the function that returns a requestId allows you to "plug in" other middleware that may set the request id
//
// Example:
//
// package main
//
// import(
// ""goji.io"
// "github.com/goji/glogrus2"
// "github.com/Sirupsen/logrus"
// )
//
// func main() {
//
// logr := logrus.New()
// logr.Formatter = new(logrus.JSONFormatter)
// goji.Use(glogrus.NewGlogrusWithReqId(logr, "my-app-name", GetRequestId))
//
// goji.Get("/ping", yourHandler)
// goji.Serve()
// }
//
// func GetRequestId(ctx context.Context) string {
// return ctx.Value("requestIdKey")
// }
//
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()
reqID := reqidf(ctx)
l.WithFields(logrus.Fields{
"req_id": reqID,
"uri": r.RequestURI,
"method": r.Method,
"remote": r.RemoteAddr,
}).Info("req_start")
lresp := wrapWriter(w)
h.ServeHTTPC(ctx, lresp, r)
lresp.maybeWriteHeader()
latency := float64(time.Since(start)) / float64(time.Millisecond)
l.WithFields(logrus.Fields{
"req_id": reqID,
"status": lresp.status(),
"method": r.Method,
"uri": r.RequestURI,
"remote": r.RemoteAddr,
"latency": fmt.Sprintf("%6.4f ms", latency),
"app": name,
}).Info("req_served")
}
return goji.HandlerFunc(fn)
}
}
func emptyRequestId(ctx context.Context) string {
return ""
}