-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathjson_logger.go
147 lines (131 loc) · 3.72 KB
/
json_logger.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package tigertonic
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
// JSONLogged is an http.Handler that logs requests and responses in a parse-able json line.
// complete with paths, statuses, headers, and bodies. Sensitive information may be
// redacted by a user-defined function.
type JSONLogger struct {
Logger Logger
handler http.Handler
redactor Redactor
RequestIDCreator RequestIDCreator
}
// JSONLogged returns an http.Handler that logs requests and responses in a parse-able json line.
// complete with paths, statuses, headers, and bodies. Sensitive information may be
// redacted by a user-defined function.
func JSONLogged(handler http.Handler, redactor Redactor) *JSONLogger {
return &JSONLogger{
Logger: log.New(os.Stdout, "", 0),
handler: handler,
redactor: redactor,
RequestIDCreator: requestIDCreator,
}
}
// ServeHTTP wraps the http.Request and http.ResponseWriter to capture the input and output for logging
func (jl *JSONLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t := time.Now()
tee := NewTeeResponseWriter(w)
rURI := r.URL.RequestURI()
body := &jsonReadCloser{r.Body, bytes.Buffer{}}
requestID := jl.RequestIDCreator(r)
r.Body = body
jl.handler.ServeHTTP(tee, r)
buf, err := json.Marshal(&jsonLog{
Duration: time.Since(t) / time.Millisecond,
HTTP: jsonLogHTTP{
Request: jsonLogHTTPRequest{
Body: body.Bytes.String(),
Header: jsonLogHTTPHeader(r.Header),
Method: r.Method,
Path: rURI,
},
Response: jsonLogHTTPResponse{
Body: tee.Body.String(),
Header: jsonLogHTTPHeader(tee.Header()),
StatusCode: tee.StatusCode,
StatusText: http.StatusText(tee.StatusCode),
},
Version: fmt.Sprintf("%d.%d", r.ProtoMajor, r.ProtoMinor),
},
Message: fmt.Sprintf(
"%s %s %s\n%s %d %s",
r.Method,
rURI,
r.Proto,
r.Proto,
tee.StatusCode,
http.StatusText(tee.StatusCode),
),
RequestID: requestID,
Type: "http",
})
if err != nil {
jl.Println(err.Error())
return
}
jl.Println("@json:", string(buf))
}
func (jl *JSONLogger) Print(v ...interface{}) {
jl.Output(2, fmt.Sprint(v...))
}
func (jl *JSONLogger) Printf(format string, v ...interface{}) {
jl.Output(2, fmt.Sprintf(format, v...))
}
func (jl *JSONLogger) Println(v ...interface{}) {
jl.Output(2, fmt.Sprintln(v...))
}
func (jl *JSONLogger) Output(calldepth int, s string) error {
if nil != jl.redactor {
s = jl.redactor(s)
}
return jl.Logger.Output(calldepth, s)
}
func jsonLogHTTPHeader(h http.Header) map[string]string {
header := make(map[string]string)
for name, values := range h {
header[strings.ToLower(name)] = strings.Join(values, "; ")
}
return header
}
type jsonLog struct {
Duration time.Duration `json:"duration"`
HTTP jsonLogHTTP `json:"http"`
Message string `json:"@message"`
RequestID RequestID `json:"@request_id"`
Type string `json:"@type"`
}
type jsonLogHTTP struct {
Request jsonLogHTTPRequest `json:"request"`
Response jsonLogHTTPResponse `json:"response"`
Version string `json:"version"`
}
type jsonLogHTTPRequest struct {
Body string `json:"body"`
Header map[string]string `json:"headers"`
Method string `json:"method"`
Path string `json:"url"`
}
type jsonLogHTTPResponse struct {
Body string `json:"body"`
Header map[string]string `json:"headers"`
StatusText string `json:"reason"`
StatusCode int `json:"status"`
}
type jsonReadCloser struct {
io.ReadCloser
Bytes bytes.Buffer
}
func (r *jsonReadCloser) Read(p []byte) (int, error) {
n, err := r.ReadCloser.Read(p)
r.Bytes.Write(p[:n])
return n, err
}