-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
104 lines (86 loc) · 2.12 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
package errors
import (
"encoding/json"
"fmt"
"time"
)
type Options struct {
skipCallers int
fields []Field
}
func (o *Options) AddField(field Field) {
o.fields = append(o.fields, field)
}
// Option is used to set error fields for structured logging and to skip caller
// for a stack trace.
type Option func(*Options)
func SkipCaller() Option {
return func(options *Options) {
options.skipCallers++
}
}
func SkipCallers(skip int) Option {
return func(options *Options) {
options.skipCallers += skip
}
}
func Bool(key string, value bool) Option {
return func(options *Options) {
options.AddField(BoolField{Key: key, Value: value})
}
}
func Int(key string, value int) Option {
return func(options *Options) {
options.AddField(IntField{Key: key, Value: value})
}
}
func Uint(key string, value uint) Option {
return func(options *Options) {
options.AddField(UintField{Key: key, Value: value})
}
}
func Float(key string, value float64) Option {
return func(options *Options) {
options.AddField(FloatField{Key: key, Value: value})
}
}
func String(key string, value string) Option {
return func(options *Options) {
options.AddField(StringField{Key: key, Value: value})
}
}
func Stringer(key string, value fmt.Stringer) Option {
return String(key, value.String())
}
func Strings(key string, values []string) Option {
return func(options *Options) {
options.AddField(StringsField{Key: key, Values: values})
}
}
func Value(key string, value interface{}) Option {
return func(options *Options) {
options.AddField(ValueField{Key: key, Value: value})
}
}
func Time(key string, value time.Time) Option {
return func(options *Options) {
options.AddField(TimeField{Key: key, Value: value})
}
}
func Duration(key string, value time.Duration) Option {
return func(options *Options) {
options.AddField(DurationField{Key: key, Value: value})
}
}
func JSON(key string, value json.RawMessage) Option {
return func(options *Options) {
options.AddField(JSONField{Key: key, Value: value})
}
}
func newOptions(options ...Option) *Options {
opts := &Options{}
for _, set := range options {
set(opts)
}
return opts
}