Compare commits
1 Commits
main
...
crawshaw/t
Author | SHA1 | Date |
---|---|---|
![]() |
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…
Reference in New Issue