-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
225 lines (184 loc) · 6.33 KB
/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package httplog
import (
"bytes"
"context"
"io"
"log/slog"
"net/http"
"runtime"
"time"
"github.com/go-chi/chi/v5/middleware"
)
func RequestLogger(logger *slog.Logger, opts *Options) func(next http.Handler) http.Handler {
return NewRequestLogger(logger, opts).Handle
}
func NewRequestLogger(logger *slog.Logger, opts *Options) *Logger {
if opts == nil {
opts = &defaultOptions
}
handler := NewHandler(logger.Handler(), opts)
return &Logger{
Logger: slog.New(handler),
opts: opts,
}
}
type Logger struct {
*slog.Logger
opts *Options
}
type Options struct {
// Level defines the verbosity of the request logs:
// slog.LevelDebug - log both request starts & responses (incl. OPTIONS)
// slog.LevelInfo - log responses (excl. OPTIONS)
// slog.LevelWarn - log 4xx and 5xx responses only (except for 429)
// slog.LevelError - log 5xx responses only
//
// Use httplog.SetLevel(ctx, slog.DebugLevel) to override the level per-request.
Level slog.Level
// Concise mode causes fewer log attributes to be printed in request logs.
// This is useful if your console is too noisy during development.
Concise bool
// RecoverPanics recovers from panics occurring in the underlying HTTP handlers
// and middlewares and returns HTTP 500 unless response status was already set.
//
// NOTE: Panics are logged as errors automatically, regardless of this setting.
RecoverPanics bool
// LogRequestHeaders is an explicit list of headers to be logged as attributes.
// If not provided, the default headers are User-Agent, Referer and Origin.
LogRequestHeaders []string
// LogRequestBody enables logging of request body into a response log attribute.
//
// Use httplog.LogRequestBody(ctx) to enable on per-request basis instead.
LogRequestBody bool
// LogRequestCURL enables logging of request body incl. all headers as a CURL command.
//
// Use httplog.LogRequestCURL(ctx) to enable on per-request basis instead.
LogRequestCURL bool
// LogResponseHeaders is an explicit list of headers to be logged as attributes.
//
// If not provided, there are no default headers.
LogResponseHeaders []string
// LogResponseBody enables logging of response body into a response log attribute.
// The Content-Type of the response must match.
//
// Use httplog.LogResponseBody(ctx) to enable on per-request basis instead.
LogResponseBody bool
// LogResponseBodyContentType defines list of Content-Types for which LogResponseBody is enabled.
//
// If not provided, the default list is application/json and text/plain.
LogResponseBodyContentType []string
}
var defaultOptions = Options{
Level: slog.LevelInfo,
RecoverPanics: true,
LogRequestHeaders: []string{"User-Agent", "Referer", "Origin"},
LogResponseHeaders: []string{""},
LogResponseBodyContentType: []string{"application/json", "text/plain"},
}
func (l *Logger) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := &log{
Level: l.opts.Level,
Req: r,
LogRequestCURL: l.opts.LogRequestCURL,
LogRequestBody: l.opts.LogRequestBody || l.opts.LogRequestCURL,
LogResponseBody: l.opts.LogResponseBody,
}
ctx = context.WithValue(ctx, logCtxKey{}, log)
l.DebugContext(ctx, "Request started")
start := time.Now()
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
log.WW = ww
if log.LogRequestBody {
r.Body = io.NopCloser(io.TeeReader(r.Body, &log.ReqBody))
}
if log.LogResponseBody {
ww.Tee(&log.RespBody)
}
defer func() {
if rec := recover(); rec != nil {
if rec != http.ErrAbortHandler {
pc := make([]uintptr, 10) // Capture up to 10 stack frames.
n := runtime.Callers(3, pc) // Skip 3 frames (this middleware + runtime/panic.go).
log.Panic = rec
log.PanicPC = pc[:n]
}
// Return HTTP 500 if recover is enabled and no response status was set.
if l.opts.RecoverPanics && ww.Status() == 0 && r.Header.Get("Connection") != "Upgrade" {
ww.WriteHeader(http.StatusInternalServerError)
}
if rec == http.ErrAbortHandler || !l.opts.RecoverPanics {
// Always re-panic http.ErrAbortHandler. Re-panic everything unless recover is enabled.
defer panic(rec)
}
}
status := ww.Status()
log.Resp = &respLog{
Header: w.Header,
Duration: time.Since(start),
}
if log.LogRequestBody {
// Make sure to read full request body if the underlying handler didn't do so.
n, _ := io.Copy(io.Discard, r.Body)
if n == 0 {
log.ReqBodyFullyRead = true
}
}
lvl := slog.LevelInfo
if r.Method == "OPTIONS" {
lvl = slog.LevelDebug
}
if status >= 500 {
lvl = slog.LevelError
} else if status == 429 {
lvl = slog.LevelInfo
} else if status >= 400 {
lvl = slog.LevelWarn
}
l.LogAttrs(ctx, lvl, "Request finished")
}()
next.ServeHTTP(ww, r.WithContext(ctx))
})
}
type log struct {
Level slog.Level
Attrs []slog.Attr
LogRequestCURL bool
LogRequestBody bool
LogResponseBody bool
// Fields set by httplog.RequestLogger middleware:
Req *http.Request
ReqBody bytes.Buffer
ReqBodyFullyRead bool
WW middleware.WrapResponseWriter
Resp *respLog
RespBody bytes.Buffer
Panic any
PanicPC []uintptr
// Fields internal to httplog:
}
type respLog struct {
Header func() http.Header
Duration time.Duration
}
// DebugContext calls [Logger.DebugContext] on the default logger.
func DebugContext(ctx context.Context, msg string, args ...any) {
slog.Default().DebugContext(ctx, msg, args...)
}
// InfoContext calls [Logger.InfoContext] on the default logger.
func InfoContext(ctx context.Context, msg string, args ...any) {
slog.Default().InfoContext(ctx, msg, args...)
}
// WarnContext calls [Logger.WarnContext] on the default logger.
func WarnContext(ctx context.Context, msg string, args ...any) {
slog.Default().WarnContext(ctx, msg, args...)
}
// ErrorContext calls [Logger.ErrorContext] on the default logger.
func ErrorContext(ctx context.Context, msg string, args ...any) {
slog.Default().ErrorContext(ctx, msg, args...)
}
// LogAttrs calls [Logger.LogAttrs] on the default logger.
func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
slog.Default().LogAttrs(ctx, level, msg, attrs...)
}