-
Notifications
You must be signed in to change notification settings - Fork 25
/
helpers.go
147 lines (128 loc) · 3.94 KB
/
helpers.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
141
142
143
144
145
146
147
package grpc
import (
"fmt"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// LogEvent wraps *zerolog.Event, so that sending an event also returns an grpc
// error
//
// LogEvent also forwards _some_ builder-functions (like Str()) to the
// underlying zerolog.Event. This is just a sugar, as you can always use
// WithEvent on the whole chain.
type LogEvent struct {
e *zerolog.Event
code codes.Code
}
// LoggedError wraps zerolog.Event so that sending an event will also return a
// grpc error.
//
// GRPC error code should be provided using Code(), otherwise, the error will
// use codes.Unknown.
//
// Example:
//
// return nil, grpc.LoggedError(log.Autosample().Warn()).
// Code(codes.InvalidArgument).
// Msg("missing frobnicator")
func LoggedError(e *zerolog.Event) LogEvent {
return LogEvent{
e: e,
code: codes.Unknown,
}
}
// Bug is equivalent to WithLogEvent(log.Bug()).Code(codes.Internal)
//
// Example: return nil, grpc.Bug().Msg("impossible happened").
func Bug() LogEvent {
// Note: not forwarding to log.Bug() as it might mess up with caller depth
// in GetAutosampler()
return LogEvent{
e: log.BugWithSampler(log.GetGlobalLogger(), log.GetAutosampler()),
code: codes.Internal,
}
}
// BugWithLogger is equivalent to WithLogEvent(logger.Bug()).Code(codes.Internal).
func BugWithLogger(lg *log.Logger) LogEvent {
// Note: not forwarding to lg.Bug() as it might mess up with caller depth
// in GetAutosampler()
return LogEvent{
e: log.BugWithSampler(lg, log.GetAutosampler()),
code: codes.Internal,
}
}
// Code sets the gRPC code to be used in error returned via Msg() or Send()
//
// Additionally, it adds the code field to the event.
func (e LogEvent) Code(code codes.Code) LogEvent {
e.e.Stringer("code", code)
e.code = code
return e
}
// Msg sends the *Event with msg added as the message field if not empty.
//
// Msg returns grpc error using given msg as message and code previously set
// with Code
//
// NOTICE: once this method is called, the LogEvent should be disposed.
// Calling Msg twice can have unexpected result.
func (e LogEvent) Msg(msg string) error {
e.e.Msg(msg)
return status.Error(e.code, msg)
}
// Send is equivalent to calling Msg("").
func (e LogEvent) Send() error {
e.e.Msg("")
return status.Error(e.code, "")
}
// *** forwarded builders ***
// Str adds the field key with val as a string to the *Event context.
func (e LogEvent) Str(key, val string) LogEvent {
e.e.Str(key, val)
return e
}
// Stringer adds the field key with val.String() (or null if val is nil)
// to the *Event context.
func (e LogEvent) Stringer(key string, val fmt.Stringer) LogEvent {
e.e.Stringer(key, val)
return e
}
// Err adds the field "error" with serialized err to the *Event context.
// If err is nil, no field is added.
//
// To customize the key name, change zerolog.ErrorFieldName.
//
// If Stack() has been called before and zerolog.ErrorStackMarshaler is defined,
// the err is passed to ErrorStackMarshaler and the result is appended to the
// zerolog.ErrorStackFieldName.
func (e LogEvent) Err(err error) LogEvent {
e.e.Err(err)
return e
}
// Bool adds the field key with val as a bool to the *Event context.
func (e LogEvent) Bool(key string, b bool) LogEvent {
e.e.Bool(key, b)
return e
}
// Int adds the field key with i as a int to the *Event context.
func (e LogEvent) Int(key string, i int) LogEvent {
e.e.Int(key, i)
return e
}
// Float32 adds the field key with f as a float32 to the *Event context.
func (e LogEvent) Float32(key string, f float32) LogEvent {
e.e.Float32(key, f)
return e
}
// Float64 adds the field key with f as a float64 to the *Event context.
func (e LogEvent) Float64(key string, f float64) LogEvent {
e.e.Float64(key, f)
return e
}
// Interface adds the field key with i marshaled using reflection.
func (e LogEvent) Interface(key string, i interface{}) LogEvent {
e.e.Interface(key, i)
return e
}