forked from thomas-tacquet/gormv2-logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
138 lines (115 loc) · 3.6 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
package gormv2logrus
import (
"time"
"github.com/sirupsen/logrus"
"gorm.io/gorm/logger"
)
// withLogrus obviously
// option to truncate loooong requests
// option do hide requests containing passwords etc...
// option log request latency
// option to change write format
// options represents all available starting options for gormv2_logrus.
// options are optional parameters that can be passed to init function of gormv2_logrus
// to configure log policy.
type options struct {
// if a query contains one of bannedKeywords, it will not be logged, it's useful for preventing passwords and secrets
// for being logged.
bannedKeywords []BannedKeyword
// pointer to your logrusEntry instance
logrusEntry *logrus.Entry
lr *logrus.Logger
SlowThreshold time.Duration
LogLevel logger.LogLevel
// if set tO 0, nothing wil be truncated, else you can set it to the value you want to avoid
// logging too big SQL queries
truncateLen uint
// if set to true, it will add latency informations for your queries
logLatency bool
Colorful bool
}
// BannedKeyword represents a rule for scanning for Keyword in log output.
type BannedKeyword struct {
// Keyword represent the string watched, for example : "password"
Keyword string
// CaseMatters if set to false, the Keyword matching will occur depending the case.
// if set to true, Keyword will stricly match input messages
CaseMatters bool
}
// Colors for Colorful option
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m"
)
type GormOptions struct {
SlowThreshold time.Duration
LogLevel logger.LogLevel
TruncateLen uint
LogLatency bool
Colorful bool
}
func defaultOptions() options {
return options{
logrusEntry: nil,
truncateLen: 0,
bannedKeywords: nil,
logLatency: false,
}
}
// Option interface is used to configure gormv2_logrus options.
type Option interface {
apply(*options)
}
// funcOption wraps a function that modifies options into an
// implementation of the Option interface.
type funcOption struct {
f func(*options)
}
// apply is used in mstack init function to read given parameters.
func (fo *funcOption) apply(do *options) {
fo.f(do)
}
// newGormLogOption is implemented by function that save parameters.
func newGormLogOption(f func(*options)) *funcOption {
return &funcOption{
f: f,
}
}
// WithLogrusEntry Option (not compatible with WithLogrus) used to specify your logrusEntry instance.
// If you don't set a logrusEntry isntance or if your logrusInstance is nil, Gormlog will consider
// that you want log to be printed on stdout.
// It's useful on developpement purposes when you want to see your logs directly in terminal.
func WithLogrusEntry(logrusEntry *logrus.Entry) Option {
return newGormLogOption(func(o *options) {
o.logrusEntry = logrusEntry
})
}
// WithLogrus Option is (not compatible with WithLogrusEntry) is used to set your logrus isntance.
func WithLogrus(lr *logrus.Logger) Option {
return newGormLogOption(func(o *options) {
o.lr = lr
})
}
func WithBannedKeyword(bannedKeywords []BannedKeyword) Option {
return newGormLogOption(func(o *options) {
o.bannedKeywords = bannedKeywords
})
}
func WithGormOptions(gormOpt GormOptions) Option {
return newGormLogOption(func(o *options) {
o.Colorful = gormOpt.Colorful
o.logLatency = gormOpt.LogLatency
o.LogLevel = gormOpt.LogLevel
o.SlowThreshold = gormOpt.SlowThreshold
})
}