mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-22 12:58:37 +00:00
tsweb: extra logging fields for HTTP handlers
Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
This commit is contained in:
parent
977381f9cc
commit
d7ffecdfc9
@ -46,6 +46,12 @@ type AccessLogRecord struct {
|
|||||||
Bytes int `json:"bytes"`
|
Bytes int `json:"bytes"`
|
||||||
// Error encountered during request processing.
|
// Error encountered during request processing.
|
||||||
Err string `json:"err"`
|
Err string `json:"err"`
|
||||||
|
|
||||||
|
// Extra contains extra fields logged by the HTTP handler.
|
||||||
|
//
|
||||||
|
// It is called "x" in the JSON both for brevity and so
|
||||||
|
// it comes last.
|
||||||
|
Extra map[string]string `json:"x,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns m as a JSON string.
|
// String returns m as a JSON string.
|
||||||
|
@ -222,6 +222,7 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
msg.Seconds = h.timeNow().Sub(msg.When).Seconds()
|
msg.Seconds = h.timeNow().Sub(msg.When).Seconds()
|
||||||
msg.Code = lw.code
|
msg.Code = lw.code
|
||||||
msg.Bytes = lw.bytes
|
msg.Bytes = lw.bytes
|
||||||
|
msg.Extra = lw.extra
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case lw.hijacked:
|
case lw.hijacked:
|
||||||
@ -264,6 +265,18 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log adds name/value to the logs from a StdHandler ResponseWriter.
|
||||||
|
func Log(w http.ResponseWriter, name, value string) {
|
||||||
|
lw, _ := w.(*loggingResponseWriter)
|
||||||
|
if lw == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if lw.extra == nil {
|
||||||
|
lw.extra = make(map[string]string)
|
||||||
|
}
|
||||||
|
lw.extra[name] = value
|
||||||
|
}
|
||||||
|
|
||||||
// loggingResponseWriter wraps a ResponseWriter and record the HTTP
|
// loggingResponseWriter wraps a ResponseWriter and record the HTTP
|
||||||
// response code that gets sent, if any.
|
// response code that gets sent, if any.
|
||||||
type loggingResponseWriter struct {
|
type loggingResponseWriter struct {
|
||||||
@ -272,6 +285,7 @@ type loggingResponseWriter struct {
|
|||||||
bytes int
|
bytes int
|
||||||
hijacked bool
|
hijacked bool
|
||||||
logf logger.Logf
|
logf logger.Logf
|
||||||
|
extra map[string]string // extra logging fields
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteHeader implements http.Handler.
|
// WriteHeader implements http.Handler.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user