-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
140 lines (115 loc) · 4.3 KB
/
options.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package errorist
import "log"
var (
globalOptions []Option
pkgLocalOptions = map[string][]Option{}
)
type Options struct {
// Logger specifies logging function used on functions end with "WithErrLog".
// It uses `log.Println` by default.
Logger func(err string)
// DetailedStacktrace specifies verbosity of stacktrace.
// If it's true, running goroutine and its traces will be dumped.
// Otherwise, it only dumps package and function name with source lines, similar to Java's.
DetailedStacktrace bool
// SkipNonProjectFiles specifies whether to skip stacktrace from non-project sources.
// true by default.
//
// It is not recommended to use this option for production because it refers
// GOPATH from environment variable for the decision. IncludedPackages is recommended.
SkipNonProjectFiles bool
// IncludedPackages specifies allowed list of package names in stacktrace.
// If set, only packages starting with given names will be included.
IncludedPackages []string
// WrapArguments specifies additional context info added on error.
// If first argument is string, it is used to format message with rest of the arguments and
// will be passed to errors.Wrapf (by default) or fmt.Errorf (optional).
WrapArguments []interface{}
// WrapWithFmtErrorf specifies whether to use fmt.Errorf on error wrapping. false by default.
WrapWithFmtErrorf bool
}
var DefaultOptions = Options{
Logger: func(err string) { log.Println(err) },
DetailedStacktrace: false,
SkipNonProjectFiles: true,
}
type Option func(o *Options)
// Wrapf is an option for adding context info to the error by wrapping it.
func Wrapf(format string, args ...interface{}) Option {
return func(o *Options) {
o.WrapArguments = []interface{}{format}
o.WrapArguments = append(o.WrapArguments, args...)
}
}
// WrapWithFmtErrorf is an option for using fmt.Errorf on error wrapping.
func WrapWithFmtErrorf() Option {
return func(o *Options) {
o.WrapWithFmtErrorf = true
}
}
// WithDetailedTrace is an option for dumping running goroutine and its traces will be dumped.
func WithDetailedTrace() Option {
return func(o *Options) {
o.DetailedStacktrace = true
}
}
// IncludeNonProjectFiles is an option for including non-project files to stacktrace.
// It is not recommended to use this option for production because it refers
// GOPATH from environment variable for the decision. IncludedPackages is recommended.
func IncludeNonProjectFiles() Option {
return func(o *Options) {
o.SkipNonProjectFiles = true
}
}
// IncludedPackages is an option specifying allowed list of package names in stacktrace.
// If set, only packages starting with given names will be included.
func IncludedPackages(pkgs ...string) Option {
return func(o *Options) {
o.IncludedPackages = pkgs
}
}
// LogrusLikeLoggingFunc includes leveled logging functions on Logrus.
// https://github.com/sirupsen/logrus#level-logging
// Other loggers sharing same function signature can be also used.
type LogrusLikeLoggingFunc func(...interface{})
// LogWithLogrus is an option for using logrus on functions end with "WithErrorLog".
func LogWithLogrus(lf LogrusLikeLoggingFunc) Option {
return func(o *Options) {
o.Logger = func(err string) { lf(err) }
}
}
type PrintfFamily func(string, ...interface{})
// LogWithPrintfFamily is an option for using printf-like loggers on functions end with "WithErrorLog".
func LogWithPrintfFamily(lf PrintfFamily) Option {
return func(o *Options) {
o.Logger = func(err string) { lf(err) }
}
}
// WithLogHandler is an option for specifying custom logger on functions end with "WithErrorLog".
func WithLogHandler(handler func(err string)) Option {
return func(o *Options) {
o.Logger = handler
}
}
// SetGlobalOptions sets options applied in current package scope.
// It can override global options.
func SetGlobalOptions(opts ...Option) {
globalOptions = opts
}
// SetPackageLevelOptions sets options applied in current package scope.
// It can override global options.
func SetPackageLevelOptions(opts ...Option) {
pkg := callerPackageName(1)
pkgLocalOptions[pkg] = opts
}
func applyOptions(opts []Option) Options {
var merged []Option
merged = append(merged, globalOptions...)
merged = append(merged, pkgLocalOptions[callerPackageName(1)]...)
merged = append(merged, opts...)
o := DefaultOptions
for _, optFn := range merged {
optFn(&o)
}
return o
}