Skip to content

Commit caf5c0f

Browse files
authored
Merge pull request #136 from snabble/panic_stack
Log stacktrace on panic in log middleware
2 parents 87f6a9a + 633f536 commit caf5c0f

File tree

6 files changed

+26
-17
lines changed

6 files changed

+26
-17
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
language: go
22
go:
3-
- 1.21.x
3+
- 1.22.x
44
before_install:
5-
- go install honnef.co/go/tools/cmd/staticcheck@latest
5+
- go install honnef.co/go/tools/cmd/staticcheck@latest
66
script:
7-
- go vet github.com/snabble/go-logging/...
8-
- go test --cover github.com/snabble/go-logging/...
9-
- staticcheck ./...
7+
- go vet github.com/snabble/go-logging/...
8+
- go test --cover github.com/snabble/go-logging/...
9+
- staticcheck ./...
1010

1111
notifications:
1212
email: false
@@ -17,4 +17,4 @@ notifications:
1717
- secure: LNfAWkWtOCKxkcaZEoh2k3zBHpZaI0Ty+VKumDuzpU2m3NYH7AxDgrFAJLZOGdAyBvMtV2K8STSVQWTLA3lNmU5m7WVFKm17J58jle3tsk2gvYIMsGOsKFy+MSmjMm4HH1NSdkFwYrAQeSvZjEywQ5qv/geJwDvIEsuppA5JCCZ6xG1GgjcptUoiSVQn7sNgz5IHCI99RjUjeRvvPlTRqFTBd4JcQvZ46jkKGccQ10+KsH1AfQ/Ay8Ns0LJKEhsmivOgakdjpSgMjnsiWhAstLoKMI/W7kCd7fP/ff8btI+zhMby75kVdgsUTnbN15Dkr8Najf6LPQ47MfFYunKTDEZhZSdXt5C0N9DwD8BpOlnzeYlmyQ3gphoF5R2a279QNIMAaOO6DUAJpcFGvsiRkjz7VRlBzZFXHNr4Sk1D+JHzISHEM1llW/m01jeEtA6TFx0raaX95To0ygl4if+WmjmfJFDrlVJUhAVxRG7ERGG1KvBzrM43Gv4L3QQr8i5Anlntbba4DfSvP5KWQAdX3JWo7feQEktH/waZ4OzKbPqGQZ5jxBhG35Sz/2o/6nUebUlaDJjMahUrddpPGcp1xlKhF5QMTK/o9kYOEy+AmYsTthAMziq41BGwTqqgqUwaLDdUOrmC3cEZWOt5spwJjzOu7FQqlu9qXXjayzsPEbg=
1818
env:
1919
global:
20-
- GOPRIVATE="github.com/snabble/*"
20+
- GOPRIVATE="github.com/snabble/*"

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/snabble/go-logging/v2
22

3-
go 1.21
3+
go 1.22
44

5-
toolchain go1.21.1
5+
toolchain go1.22.0
66

77
require (
88
github.com/sirupsen/logrus v1.9.3

helper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ func isHealthRequest(r *http.Request) bool {
101101
}
102102

103103
// AccessError logs an error while accessing
104-
func AccessError(r *http.Request, start time.Time, err error) {
104+
func AccessError(r *http.Request, start time.Time, err error, stack []byte) {
105105
e := createAccessEntry(r, start, 0, err)
106+
107+
if stack != nil {
108+
e = e.WithField("stack", string(stack))
109+
}
110+
106111
e.Errorf("ERROR ->%v %v", r.Method, r.URL.Path)
107112
}
108113

log_middleware.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"regexp"
88
"runtime"
9+
"runtime/debug"
910
"strings"
1011
"time"
1112

@@ -77,7 +78,7 @@ func (mw *LogMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7778
AccessAborted(r, start)
7879
return
7980
}
80-
AccessError(r, start, fmt.Errorf("PANIC (%v): %v", identifyLogOrigin(), rec))
81+
AccessError(r, start, fmt.Errorf("PANIC (%v): %v", identifyLogOrigin(), rec), debug.Stack())
8182
}
8283
}()
8384

log_middleware_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ func Test_LogMiddleware_Panic(t *testing.T) {
8989

9090
assert.Contains(t, data.Error, "Test_LogMiddleware_Panic.func1")
9191
assert.Contains(t, data.Error, "some")
92-
assert.Equal(t, data.Message, "ERROR ->GET /foo")
93-
assert.Equal(t, data.Level, "error")
92+
assert.Equal(t, "ERROR ->GET /foo", data.Message)
93+
assert.Equal(t, "error", data.Level)
94+
assert.NotZero(t, data.Stack)
9495
}
9596

9697
func Test_LogMiddleware_Panic_ErrAbortHandler(t *testing.T) {
@@ -112,8 +113,8 @@ func Test_LogMiddleware_Panic_ErrAbortHandler(t *testing.T) {
112113

113114
data := logRecordFromBuffer(b)
114115

115-
assert.Equal(t, data.Message, "ABORTED ->GET /foo")
116-
assert.Equal(t, data.Level, "info")
116+
assert.Equal(t, "ABORTED ->GET /foo", data.Message)
117+
assert.Equal(t, "info", data.Level)
117118
}
118119

119120
func Test_LogMiddleware_Log_implicit200(t *testing.T) {
@@ -135,7 +136,7 @@ func Test_LogMiddleware_Log_implicit200(t *testing.T) {
135136
data := logRecordFromBuffer(b)
136137
a.Equal("", data.Error)
137138
a.Equal("200 ->GET /foo", data.Message)
138-
a.Equal(200, data.ResponseStatus)
139+
a.Equal(http.StatusOK, data.ResponseStatus)
139140
a.Equal("info", data.Level)
140141
}
141142

@@ -225,7 +226,7 @@ func Test_LogMiddleware_Log_404(t *testing.T) {
225226
data := logRecordFromBuffer(b)
226227
a.Equal("", data.Error)
227228
a.Equal("404 ->GET /foo", data.Message)
228-
a.Equal(404, data.ResponseStatus)
229+
a.Equal(http.StatusNotFound, data.ResponseStatus)
229230
a.Equal("warning", data.Level)
230231
}
231232

logger_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type logRecord struct {
3232
UserAgent string `json:"User_Agent"`
3333
SpanID string `json:"span"`
3434
TraceID string `json:"trace"`
35+
Stack string `json:"stack"`
3536
}
3637

3738
func Test_Logger_Set(t *testing.T) {
@@ -215,12 +216,13 @@ func Test_Logger_Access_ErrorCases(t *testing.T) {
215216

216217
// when an error is logged
217218
b.Reset()
218-
AccessError(r, time.Now(), errors.New("oops"))
219+
AccessError(r, time.Now(), errors.New("oops"), []byte("__stacktrace__"))
219220
// then: all fields match
220221
data = logRecordFromBuffer(b)
221222
a.Equal("error", data.Level)
222223
a.Equal("oops", data.Error)
223224
a.Equal("ERROR ->GET /foo", data.Message)
225+
a.Equal("__stacktrace__", data.Stack)
224226
}
225227

226228
func Test_Logger_LifecycleStart(t *testing.T) {

0 commit comments

Comments
 (0)