Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

80 linhas
1.7 KiB

  1. package internal
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. // TODO (ned): Revisit logging
  9. // Add more standardized approach with log levels and configurability
  10. type Logging interface {
  11. Printf(ctx context.Context, format string, v ...interface{})
  12. }
  13. type DefaultLogger struct {
  14. log *log.Logger
  15. }
  16. func (l *DefaultLogger) Printf(ctx context.Context, format string, v ...interface{}) {
  17. _ = l.log.Output(2, fmt.Sprintf(format, v...))
  18. }
  19. func NewDefaultLogger() Logging {
  20. return &DefaultLogger{
  21. log: log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile),
  22. }
  23. }
  24. // Logger calls Output to print to the stderr.
  25. // Arguments are handled in the manner of fmt.Print.
  26. var Logger Logging = NewDefaultLogger()
  27. var LogLevel LogLevelT = LogLevelError
  28. // LogLevelT represents the logging level
  29. type LogLevelT int
  30. // Log level constants for the entire go-redis library
  31. const (
  32. LogLevelError LogLevelT = iota // 0 - errors only
  33. LogLevelWarn // 1 - warnings and errors
  34. LogLevelInfo // 2 - info, warnings, and errors
  35. LogLevelDebug // 3 - debug, info, warnings, and errors
  36. )
  37. // String returns the string representation of the log level
  38. func (l LogLevelT) String() string {
  39. switch l {
  40. case LogLevelError:
  41. return "ERROR"
  42. case LogLevelWarn:
  43. return "WARN"
  44. case LogLevelInfo:
  45. return "INFO"
  46. case LogLevelDebug:
  47. return "DEBUG"
  48. default:
  49. return "UNKNOWN"
  50. }
  51. }
  52. // IsValid returns true if the log level is valid
  53. func (l LogLevelT) IsValid() bool {
  54. return l >= LogLevelError && l <= LogLevelDebug
  55. }
  56. func (l LogLevelT) WarnOrAbove() bool {
  57. return l >= LogLevelWarn
  58. }
  59. func (l LogLevelT) InfoOrAbove() bool {
  60. return l >= LogLevelInfo
  61. }
  62. func (l LogLevelT) DebugOrAbove() bool {
  63. return l >= LogLevelDebug
  64. }