...
1
2
3
4
5
6
7
8
9
10
11
12 package main
13
14 import (
15 "fmt"
16 "net/http"
17 "time"
18
19 "github.com/go-chi/chi"
20 "github.com/go-chi/chi/middleware"
21 "github.com/sirupsen/logrus"
22 )
23
24 func main() {
25
26
27
28
29 logger := logrus.New()
30 logger.Formatter = &logrus.JSONFormatter{
31
32 DisableTimestamp: true,
33 }
34
35
36 r := chi.NewRouter()
37 r.Use(middleware.RequestID)
38 r.Use(NewStructuredLogger(logger))
39 r.Use(middleware.Recoverer)
40
41 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
42 w.Write([]byte("welcome"))
43 })
44 r.Get("/wait", func(w http.ResponseWriter, r *http.Request) {
45 time.Sleep(1 * time.Second)
46 LogEntrySetField(r, "wait", true)
47 w.Write([]byte("hi"))
48 })
49 r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
50 panic("oops")
51 })
52 http.ListenAndServe(":3333", r)
53 }
54
55
56
57
58
59
60 func NewStructuredLogger(logger *logrus.Logger) func(next http.Handler) http.Handler {
61 return middleware.RequestLogger(&StructuredLogger{logger})
62 }
63
64 type StructuredLogger struct {
65 Logger *logrus.Logger
66 }
67
68 func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry {
69 entry := &StructuredLoggerEntry{Logger: logrus.NewEntry(l.Logger)}
70 logFields := logrus.Fields{}
71
72 logFields["ts"] = time.Now().UTC().Format(time.RFC1123)
73
74 if reqID := middleware.GetReqID(r.Context()); reqID != "" {
75 logFields["req_id"] = reqID
76 }
77
78 scheme := "http"
79 if r.TLS != nil {
80 scheme = "https"
81 }
82 logFields["http_scheme"] = scheme
83 logFields["http_proto"] = r.Proto
84 logFields["http_method"] = r.Method
85
86 logFields["remote_addr"] = r.RemoteAddr
87 logFields["user_agent"] = r.UserAgent()
88
89 logFields["uri"] = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
90
91 entry.Logger = entry.Logger.WithFields(logFields)
92
93 entry.Logger.Infoln("request started")
94
95 return entry
96 }
97
98 type StructuredLoggerEntry struct {
99 Logger logrus.FieldLogger
100 }
101
102 func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
103 l.Logger = l.Logger.WithFields(logrus.Fields{
104 "resp_status": status, "resp_bytes_length": bytes,
105 "resp_elapsed_ms": float64(elapsed.Nanoseconds()) / 1000000.0,
106 })
107
108 l.Logger.Infoln("request complete")
109 }
110
111 func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) {
112 l.Logger = l.Logger.WithFields(logrus.Fields{
113 "stack": string(stack),
114 "panic": fmt.Sprintf("%+v", v),
115 })
116 }
117
118
119
120
121
122
123
124
125 func GetLogEntry(r *http.Request) logrus.FieldLogger {
126 entry := middleware.GetLogEntry(r).(*StructuredLoggerEntry)
127 return entry.Logger
128 }
129
130 func LogEntrySetField(r *http.Request, key string, value interface{}) {
131 if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok {
132 entry.Logger = entry.Logger.WithField(key, value)
133 }
134 }
135
136 func LogEntrySetFields(r *http.Request, fields map[string]interface{}) {
137 if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok {
138 entry.Logger = entry.Logger.WithFields(fields)
139 }
140 }
141
View as plain text