github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/logger/debugger.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/redis/go-redis/v9" 8 ) 9 10 var debugger Debugger 11 12 // Debugger manage the list of domains with the debug mode. 13 // 14 // Once you call `AddDomain` all Debug logs containing the 15 // corresponding `domain` field (setup with `WithDomain`) 16 // will be printed even if the global logger is setup with 17 // a higher level (like 'info'). 18 type Debugger interface { 19 AddDomain(domain string, ttl time.Duration) error 20 RemoveDomain(domain string) error 21 ExpiresAt(domain string) *time.Time 22 } 23 24 func initDebugger(client redis.UniversalClient) error { 25 var err error 26 27 if client == nil { 28 debugger = NewMemDebugger() 29 return nil 30 } 31 32 debugger, err = NewRedisDebugger(client) 33 if err != nil { 34 return fmt.Errorf("failed to init the redis debugger: %w", err) 35 } 36 37 return nil 38 } 39 40 // AddDebugDomain adds the specified domain to the debug list. 41 func AddDebugDomain(domain string, ttl time.Duration) error { 42 return debugger.AddDomain(domain, ttl) 43 } 44 45 // RemoveDebugDomain removes the specified domain from the debug list. 46 func RemoveDebugDomain(domain string) error { 47 return debugger.RemoveDomain(domain) 48 } 49 50 // DebugExpiration returns the expiration date for the debug mode for the 51 // instance logger of the given domain (or nil if the debug mode is not 52 // activated). 53 func DebugExpiration(domain string) *time.Time { 54 return debugger.ExpiresAt(domain) 55 }