forked from ciricc/zerolog-grpc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
81 lines (66 loc) · 2.05 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
package zerologgrpcprovider
import (
"github.com/rs/zerolog"
)
type Options struct {
// Whether need to log requests or not
logRequests bool
// Whether need to log errors or not
logErrors bool
// Whether need push request fields to log or not
provideRequestFieldsToLogger bool
// Whether need generate request id in the log or not
useRequestId bool
// Zerolog logger
requestLogger *zerolog.Logger
// Function which can modify request values before logging into output
requestValueModifier RequestValueModifier
}
type Option func(opts *Options) error
// When logging is true, the logger will print messages by self into zerolog output.
// Default value is true
func WithLogRequests(logging bool) Option {
return func(opts *Options) error {
opts.logRequests = logging
return nil
}
}
// Add request value modifier for modifying request values before they are sent
// to the output
func WithRequestValueModifier(modifier RequestValueModifier) Option {
return func(opts *Options) error {
opts.requestValueModifier = modifier
return nil
}
}
// When logging errors is true, the logger will print errors after request completion.
// Default value is true
func WithLogErrors(logging bool) Option {
return func(opts *Options) error {
opts.logErrors = logging
return nil
}
}
// When provideFields is enabled, provider will add into zerolog context some request fields
// Like grpcMethod, grpcServer information etc.
func WithProvideRequestFieldsToLogger(provideFields bool) Option {
return func(opts *Options) error {
opts.provideRequestFieldsToLogger = provideFields
return nil
}
}
// When use request id is enabled, interceptor will generate a request identifier in the UUID format
// and will add this into zerolog context fields list
func WithUseRequestId(useRequestId bool) Option {
return func(opts *Options) error {
opts.useRequestId = useRequestId
return nil
}
}
// WithLogger changes the default zerolog logger
func WithLogger(logger *zerolog.Logger) Option {
return func(opts *Options) error {
opts.requestLogger = logger
return nil
}
}