github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/ezdbg/config.go (about) 1 package ezdbg 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "os" 8 9 "github.com/jxskiss/gopkg/v2/internal/logfilter" 10 ) 11 12 const FilterRuleEnvName = "EZDBG_FILTER_RULE" 13 14 // Config configures the behavior of functions in this package. 15 func Config(cfg Cfg) { 16 if cfg.FilterRule == "" { 17 envRule := os.Getenv(FilterRuleEnvName) 18 if envRule != "" { 19 stdLogger{}.Infof("ezdbg: using filter rule from env: %q", envRule) 20 cfg.FilterRule = envRule 21 } 22 } 23 if cfg.FilterRule != "" { 24 var errs []error 25 cfg.filter, errs = logfilter.NewFileNameFilter(cfg.FilterRule) 26 for _, err := range errs { 27 stdLogger{}.Warnf("ezdbg: %v", err) 28 } 29 } 30 _logcfg = cfg 31 } 32 33 var _logcfg Cfg 34 35 // Cfg provides optional config to configure this package. 36 type Cfg struct { 37 38 // EnableDebug determines whether debug log is enabled, it may use 39 // the given context.Context to enable or disable request-level debug log. 40 // If EnableDebug returns false, the log message is discarded. 41 // 42 // User must configure this to enable debug log from this package. 43 // By default, functions in this package discard all messages. 44 EnableDebug func(context.Context) bool 45 46 // LoggerFunc optionally retrieves DebugLogger from a context.Context. 47 LoggerFunc func(context.Context) DebugLogger 48 49 // FilterRule optionally configures filter rule to allow or deny log messages 50 // in some packages or files. 51 // 52 // It uses glob to match filename, the syntax is "allow=glob1,glob2;deny=glob3,glob4". 53 // For example: 54 // 55 // - "", empty rule means allow all messages 56 // - "allow=all", allow all messages 57 // - "deny=all", deny all messages 58 // - "allow=pkg1/*,pkg2/*.go", 59 // allow messages from files in `pkg1` and `pkg2`, 60 // deny messages from all other packages 61 // - "allow=pkg1/sub1/abc.go,pkg1/sub2/def.go", 62 // allow messages from file `pkg1/sub1/abc.go` and `pkg1/sub2/def.go`, 63 // deny messages from all other files 64 // - "allow=pkg1/**", 65 // allow messages from files and sub-packages in `pkg1`, 66 // deny messages from all other packages 67 // - "deny=pkg1/**.go,pkg2/**.go", 68 // deny messages from files and sub-packages in `pkg1` and `pkg2`, 69 // allow messages from all other packages 70 // - "allow=all;deny=pkg/**", same as "deny=pkg/**" 71 // 72 // If both "allow" and "deny" directives are configured, the "allow" directive 73 // takes effect, the "deny" directive is ignored. 74 // 75 // The default value is empty, which means all messages are allowed. 76 // 77 // User can also set the environment variable "EZDBG_FILTER_RULE" 78 // to configure it at runtime, if available, the environment variable 79 // is used when this value is empty. 80 FilterRule string 81 82 filter *logfilter.FileNameFilter 83 } 84 85 func (p Cfg) getLogger(ctxp *context.Context) DebugLogger { 86 ctx := context.Background() 87 if ctxp != nil && *ctxp != nil { 88 ctx = *ctxp 89 } 90 if p.LoggerFunc != nil { 91 if lg := p.LoggerFunc(ctx); lg != nil { 92 return lg 93 } 94 } 95 return stdLogger{} 96 } 97 98 // DebugLogger is an interface which log an message at DEBUG level. 99 // It's implemented by *logrus.Logger, *logrus.Entry, *zap.SugaredLogger, 100 // and many other logging packages. 101 type DebugLogger interface { 102 Debugf(format string, args ...any) 103 } 104 105 // PrintFunc is a function to print the given arguments in format to somewhere. 106 // It implements the interface `ErrDebugLogger`. 107 type PrintFunc func(format string, args ...any) 108 109 func (f PrintFunc) Debugf(format string, args ...any) { f(format, args...) } 110 111 type stdLogger struct{} 112 113 const ( 114 stdLogDepth = 2 115 stdDebugPrefix = "[DEBUG] " 116 stdInfoPrefix = "[INFO] " 117 stdWarnPrefix = "[WARN] " 118 ) 119 120 func (stdLogger) Debugf(format string, args ...any) { 121 log.Default().Output(stdLogDepth, fmt.Sprintf(stdDebugPrefix+format, args...)) 122 } 123 124 func (stdLogger) Infof(format string, args ...any) { 125 log.Default().Output(stdLogDepth, fmt.Sprintf(stdInfoPrefix+format, args...)) 126 } 127 128 func (stdLogger) Warnf(format string, args ...any) { 129 log.Default().Output(stdLogDepth, fmt.Sprintf(stdWarnPrefix+format, args...)) 130 }