forked from blendle/zapdriver
-
Notifications
You must be signed in to change notification settings - Fork 8
/
error_test.go
86 lines (72 loc) · 2.39 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package zapdriver
import (
"os"
"regexp"
"runtime"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
type fakeErr struct{}
// manually set the frames to allow asserting stacktraces
func (fakeErr) StackTrace() errors.StackTrace {
pc1, _, _, _ := runtime.Caller(0)
pc2, _, _, _ := runtime.Caller(0)
return []errors.Frame{
errors.Frame(pc1),
errors.Frame(pc2),
}
}
func (fakeErr) Error() string {
return "fake error: underlying error"
}
/*
func TestFmtStack(t *testing.T) {
stacktrace := stackdriverFmtError{fakeErr{}}.Error()
assert.Equal(t, `fake error: underlying error
goroutine 1 [running]:
github.com/blendle/zapdriver.fakeErr.StackTrace()
/error_test.go:18 +0x1337
github.com/blendle/zapdriver.fakeErr.StackTrace()
/error_test.go:19 +0x1337`, makeStackTraceStable(stacktrace))
}
*/
// cleanup local paths & local function pointers
func makeStackTraceStable(str string) string {
re := regexp.MustCompile(`(?m)^\t.+(\/\S+:\d+) \+0x.+$`)
str = re.ReplaceAllString(str, "\t${1} +0x1337")
dir, _ := os.Getwd()
str = strings.ReplaceAll(str, dir, "")
return str
}
func ExampleSkipFmtStackTraces() {
logger, _ := NewProduction()
logger.Error("with exception", zap.Error(errors.New("internal error")), ErrorReport(runtime.Caller(0)))
logger, _ = NewProduction(WrapCore(ServiceName("service"), ReportAllErrors(true)))
logger.Error("with exception", zap.Error(errors.New("internal error")))
logger, _ = NewProduction(WrapCore(ServiceName("service"), SkipFmtStackTraces(true)))
logger.Error("without exception", zap.Error(errors.New("internal error")))
// Output:
}
func TestStackTrace(t *testing.T) {
config := NewProductionConfig()
logger, err := config.Build()
require.NoError(t, err)
core, logs := observer.New(zap.InfoLevel)
logger = logger.WithOptions(zap.WrapCore(func(zapcore.Core) zapcore.Core {
return core
}), WrapCore(ReportAllErrors(true)))
err = errors.New("internal error")
logger.Error("error", zap.Error(err))
logger.Sugar().With(zap.Error(err)).Error("error2")
logger.Sugar().Info("test3")
logger.Sugar().Error("test4")
require.NotEmpty(t, logs.All()[0].ContextMap()["exception"])
require.NotEmpty(t, logs.All()[1].ContextMap()["exception"])
require.Empty(t, logs.All()[2].ContextMap()["exception"])
require.Empty(t, logs.All()[3].ContextMap()["exception"])
}