Skip to content

Commit 2c5db64

Browse files
authored
Merge pull request #16 from snabble/add-stacktraces-to-log-entries
Add common stacktrace support for go-logging. Needs pretty printing.
2 parents a4bed2c + 937a85f commit 2c5db64

File tree

5 files changed

+354
-4
lines changed

5 files changed

+354
-4
lines changed

go.mod

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

3-
go 1.14
3+
go 1.17
44

55
require (
6-
github.com/jinzhu/now v1.1.2 // indirect
76
github.com/sirupsen/logrus v1.8.1
87
github.com/stretchr/testify v1.3.0
9-
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d // indirect
8+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
109
gorm.io/gorm v1.21.4
1110
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/jinzhu/inflection v1.0.0 // indirect
15+
github.com/jinzhu/now v1.1.2 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d // indirect
18+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
1717
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1818
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0GwFZoHR0hOrTg4qH8GA=
1919
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
21+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2022
gorm.io/gorm v1.21.4 h1:J0xfPJMRfHgpVcYLrEAIqY/apdvTIkrltPQNHQLq9Qc=
2123
gorm.io/gorm v1.21.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=

logger.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package logging
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"github.com/sirupsen/logrus"
@@ -75,7 +76,12 @@ func NewEntry(logger *Logger) *Entry {
7576
}
7677

7778
func (entry *Entry) WithError(err error) *Entry {
78-
return entry.WithField(logrus.ErrorKey, err)
79+
withError := entry.WithField(logrus.ErrorKey, err)
80+
stacktrace := ExtractStacktrace(err)
81+
if stacktrace != nil {
82+
return withError.WithField("stacktrace", fmt.Sprintf("%+v", stacktrace))
83+
}
84+
return withError
7985
}
8086

8187
func (entry *Entry) WithContext(ctx context.Context) *Entry {

logger_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/sirupsen/logrus"
1313
"github.com/stretchr/testify/assert"
14+
"golang.org/x/xerrors"
1415
)
1516

1617
type logReccord struct {
@@ -51,6 +52,25 @@ func Test_Logger_Set(t *testing.T) {
5152
a.Regexp(`^time.* level\=error msg\=oops foo\=bar.*`, b.String())
5253
}
5354

55+
func Test_Logger_WithError(t *testing.T) {
56+
a := assert.New(t)
57+
58+
// given: an logger in text format
59+
Set("info", true)
60+
defer Set("info", false)
61+
Log.Formatter.(*logrus.TextFormatter).DisableColors = true
62+
b := bytes.NewBuffer(nil)
63+
Log.Out = b
64+
65+
err := func() error {
66+
return xerrors.Errorf("found an error: %w", errors.New("an error occurred"))
67+
}()
68+
Log.WithError(err).Error("oops")
69+
70+
print( b.String())
71+
a.Regexp(`^time.* level\=error msg\=oops error\="found an error: an error occurred" stacktrace\=".*"`, b.String())
72+
}
73+
5474
func Test_Logger_Call(t *testing.T) {
5575
a := assert.New(t)
5676

0 commit comments

Comments
 (0)