From 3f5c8cea573c8948cb870a48a80e8e3e3391342b Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 19:05:40 -0800 Subject: [PATCH] don't log file/line for access log type --- http_logging.go | 12 +++++++++--- http_logging_test.go | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/http_logging.go b/http_logging.go index bff5d96..0782c10 100644 --- a/http_logging.go +++ b/http_logging.go @@ -19,6 +19,7 @@ import ( "fmt" "log" "net/http" + "runtime/debug" "sort" "strings" "time" @@ -101,7 +102,8 @@ func LogRequest(r *http.Request, msg string, extraAttributes ...KeyVal) { attr = append(attr, Str(nl, strings.Join(r.Header[name], ","))) } } - S(Info, msg, attr...) + // not point in having the line number be this file + s(Info, false, Config.JSON, msg, attr...) } // LogResponse logs the response code, byte size and duration of the request. @@ -127,7 +129,8 @@ func LogResponse[T *ResponseRecorder | *http.Response](r T, msg string, extraAtt Int64("size", size), } attr = append(attr, extraAttributes...) - S(Info, msg, attr...) + // not point in having the line number be this file + s(Info, false, Config.JSON, msg, attr...) } // Can be used (and is used by LogAndCall()) to wrap a http.ResponseWriter to record status code and size. @@ -182,7 +185,10 @@ func LogAndCall(msg string, handlerFunc http.HandlerFunc, extraAttributes ...Key respRec := &ResponseRecorder{w: w, startTime: time.Now()} defer func() { if err := recover(); err != nil { - S(Critical, "panic in handler", Any("error", err)) + s(Critical, false, Config.JSON, "panic in handler", Any("error", err)) + if Log(Verbose) { + s(Verbose, false, Config.JSON, "stack trace", Str("stack", string(debug.Stack()))) + } } attr := []KeyVal{ Int("status", respRec.StatusCode), diff --git a/http_logging_test.go b/http_logging_test.go index dc24dc6..7bec558 100644 --- a/http_logging_test.go +++ b/http_logging_test.go @@ -76,7 +76,7 @@ func (n *NullHTTPWriter) Flush() { func (n *NullHTTPWriter) WriteHeader(_ int) {} func TestLogAndCall(t *testing.T) { - Config.LogFileAndLine = false + Config.LogFileAndLine = true // yet won't show up in output Config.JSON = true Config.NoTimestamp = true Config.CombineRequestAndResponse = false // Separate request and response logging @@ -122,6 +122,7 @@ func TestLogAndCall(t *testing.T) { } n.doPanic = true n.doErr = false + SetLogLevelQuiet(Verbose) b.Reset() LogAndCall("test-log-and-call4", testHandler).ServeHTTP(hw, hr) w.Flush()