forked from gin-contrib/logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
99 lines (82 loc) · 2.22 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
package logger
import (
"io"
"regexp"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
)
// Option specifies instrumentation configuration options.
type Option interface {
apply(*config)
}
var _ Option = (*optionFunc)(nil)
type optionFunc func(*config)
func (o optionFunc) apply(c *config) {
o(c)
}
// WithLogger set custom logger func
func WithLogger(fn func(*gin.Context, zerolog.Logger) zerolog.Logger) Option {
return optionFunc(func(c *config) {
c.logger = fn
})
}
// WithSkipPathRegexps multiple skip URL paths by regexp pattern
func WithSkipPathRegexps(regs ...*regexp.Regexp) Option {
return optionFunc(func(c *config) {
if len(regs) == 0 {
return
}
c.skipPathRegexps = append(c.skipPathRegexps, regs...)
})
}
// WithUTC returns t with the location set to UTC.
func WithUTC(s bool) Option {
return optionFunc(func(c *config) {
c.utc = s
})
}
// WithSkipPath skip URL path by specific pattern
func WithSkipPath(s []string) Option {
return optionFunc(func(c *config) {
c.skipPath = s
})
}
// WithPathLevel use logging level for successful requests to a specific path
func WithPathLevel(m map[string]zerolog.Level) Option {
return optionFunc(func(c *config) {
c.pathLevels = m
})
}
// WithWriter change the default output writer.
// Default is gin.DefaultWriter
func WithWriter(s io.Writer) Option {
return optionFunc(func(c *config) {
c.output = s
})
}
// WithDefaultLevel set the log level used for request with status code < 400
func WithDefaultLevel(lvl zerolog.Level) Option {
return optionFunc(func(c *config) {
c.defaultLevel = lvl
})
}
// WithClientErrorLevel set the log level used for request with status code between 400 and 499
func WithClientErrorLevel(lvl zerolog.Level) Option {
return optionFunc(func(c *config) {
c.clientErrorLevel = lvl
})
}
// WithServerErrorLevel set the log level used for request with status code >= 500
func WithServerErrorLevel(lvl zerolog.Level) Option {
return optionFunc(func(c *config) {
c.serverErrorLevel = lvl
})
}
// WithSkipper set function to skip middleware
// requests with this function returning true will not have their logs written
// Default is nil
func WithSkipper(s Skipper) Option {
return optionFunc(func(c *config) {
c.skip = s
})
}