-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
130 lines (111 loc) · 3.86 KB
/
config.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
package ezdbg
import (
"context"
"fmt"
"log"
"os"
"github.com/jxskiss/gopkg/v2/internal/logfilter"
)
const FilterRuleEnvName = "EZDBG_FILTER_RULE"
// Config configures the behavior of functions in this package.
func Config(cfg Cfg) {
if cfg.FilterRule == "" {
envRule := os.Getenv(FilterRuleEnvName)
if envRule != "" {
stdLogger{}.Infof("ezdbg: using filter rule from env: %q", envRule)
cfg.FilterRule = envRule
}
}
if cfg.FilterRule != "" {
var errs []error
cfg.filter, errs = logfilter.NewFileNameFilter(cfg.FilterRule)
for _, err := range errs {
stdLogger{}.Warnf("ezdbg: %v", err)
}
}
_logcfg = cfg
}
var _logcfg Cfg
// Cfg provides optional config to configure this package.
type Cfg struct {
// EnableDebug determines whether debug log is enabled, it may use
// the given context.Context to enable or disable request-level debug log.
// If EnableDebug returns false, the log message is discarded.
//
// User must configure this to enable debug log from this package.
// By default, functions in this package discard all messages.
EnableDebug func(context.Context) bool
// LoggerFunc optionally retrieves DebugLogger from a context.Context.
LoggerFunc func(context.Context) DebugLogger
// FilterRule optionally configures filter rule to allow or deny log messages
// in some packages or files.
//
// It uses glob to match filename, the syntax is "allow=glob1,glob2;deny=glob3,glob4".
// For example:
//
// - "", empty rule means allow all messages
// - "allow=all", allow all messages
// - "deny=all", deny all messages
// - "allow=pkg1/*,pkg2/*.go",
// allow messages from files in `pkg1` and `pkg2`,
// deny messages from all other packages
// - "allow=pkg1/sub1/abc.go,pkg1/sub2/def.go",
// allow messages from file `pkg1/sub1/abc.go` and `pkg1/sub2/def.go`,
// deny messages from all other files
// - "allow=pkg1/**",
// allow messages from files and sub-packages in `pkg1`,
// deny messages from all other packages
// - "deny=pkg1/**.go,pkg2/**.go",
// deny messages from files and sub-packages in `pkg1` and `pkg2`,
// allow messages from all other packages
// - "allow=all;deny=pkg/**", same as "deny=pkg/**"
//
// If both "allow" and "deny" directives are configured, the "allow" directive
// takes effect, the "deny" directive is ignored.
//
// The default value is empty, which means all messages are allowed.
//
// User can also set the environment variable "EZDBG_FILTER_RULE"
// to configure it at runtime, if available, the environment variable
// is used when this value is empty.
FilterRule string
filter *logfilter.FileNameFilter
}
func (p Cfg) getLogger(ctxp *context.Context) DebugLogger {
ctx := context.Background()
if ctxp != nil && *ctxp != nil {
ctx = *ctxp
}
if p.LoggerFunc != nil {
if lg := p.LoggerFunc(ctx); lg != nil {
return lg
}
}
return stdLogger{}
}
// DebugLogger is an interface which log an message at DEBUG level.
// It's implemented by *logrus.Logger, *logrus.Entry, *zap.SugaredLogger,
// and many other logging packages.
type DebugLogger interface {
Debugf(format string, args ...any)
}
// PrintFunc is a function to print the given arguments in format to somewhere.
// It implements the interface `ErrDebugLogger`.
type PrintFunc func(format string, args ...any)
func (f PrintFunc) Debugf(format string, args ...any) { f(format, args...) }
type stdLogger struct{}
const (
stdLogDepth = 2
stdDebugPrefix = "[DEBUG] "
stdInfoPrefix = "[INFO] "
stdWarnPrefix = "[WARN] "
)
func (stdLogger) Debugf(format string, args ...any) {
log.Default().Output(stdLogDepth, fmt.Sprintf(stdDebugPrefix+format, args...))
}
func (stdLogger) Infof(format string, args ...any) {
log.Default().Output(stdLogDepth, fmt.Sprintf(stdInfoPrefix+format, args...))
}
func (stdLogger) Warnf(format string, args ...any) {
log.Default().Output(stdLogDepth, fmt.Sprintf(stdWarnPrefix+format, args...))
}