Skip to content

Commit

Permalink
fix(logrusx): emit stack traces on debug (ory#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Jan 18, 2022
1 parent 7112e26 commit 8eeb4d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions logrusx/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ func (l *Logger) WithError(err error) *Logger {
}

ctx := map[string]interface{}{"message": err.Error()}
if l.Entry.Logger.IsLevelEnabled(logrus.TraceLevel) {
if l.Entry.Logger.IsLevelEnabled(logrus.DebugLevel) {
if e, ok := err.(errorsx.StackTracer); ok {
ctx["trace"] = fmt.Sprintf("%+v", e.StackTrace())
ctx["stack_trace"] = fmt.Sprintf("%+v", e.StackTrace())
} else {
ctx["trace"] = fmt.Sprintf("stack trace could not be recovered from error type %s", reflect.TypeOf(err))
ctx["stack_trace"] = fmt.Sprintf("stack trace could not be recovered from error type %s", reflect.TypeOf(err))
}
}
if c := errorsx.ReasonCarrier(nil); errors.As(err, &c) {
Expand Down
9 changes: 9 additions & 0 deletions logrusx/logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestTextLogger(t *testing.T) {
audit := NewAudit("logrusx-audit", "v0.0.0", ForceFormat("text"), ForceLevel(logrus.TraceLevel))
tracer := New("logrusx-app", "v0.0.0", ForceFormat("text"), ForceLevel(logrus.TraceLevel))
debugger := New("logrusx-server", "v0.0.1", ForceFormat("text"), ForceLevel(logrus.DebugLevel))
warner := New("logrusx-server", "v0.0.1", ForceFormat("text"), ForceLevel(logrus.WarnLevel))
for k, tc := range []struct {
l *Logger
expect []string
Expand Down Expand Up @@ -144,6 +145,14 @@ func TestTextLogger(t *testing.T) {
},
{
l: debugger,
expect: []string{"audience=application", "service_name=logrusx-server", "service_version=v0.0.1",
"An error occurred.", "message:some error"},
call: func(l *Logger) {
l.WithError(errors.New("some error")).Error("An error occurred.")
},
},
{
l: warner,
expect: []string{"audience=application", "service_name=logrusx-server", "service_version=v0.0.1",
"An error occurred.", "message:some error"},
notExpect: []string{"logrus_test.go", "logrusx_test.TestTextLogger", "trace", "testing.tRunner"},
Expand Down

0 comments on commit 8eeb4d6

Please sign in to comment.