log.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package log
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. )
  9. // baseLogger defines the minimal logger functionality required by the package.
  10. // It allows storing any logger implementation regardless of its generic type.
  11. type baseLogger interface {
  12. CommonLogger
  13. SetLevel(Level)
  14. SetOutput(io.Writer)
  15. WithContext(ctx context.Context) CommonLogger
  16. }
  17. var logger baseLogger = &defaultLogger{
  18. stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
  19. depth: 4,
  20. }
  21. // Logger is a logger interface that provides logging function with levels.
  22. type Logger interface {
  23. Trace(v ...any)
  24. Debug(v ...any)
  25. Info(v ...any)
  26. Warn(v ...any)
  27. Error(v ...any)
  28. Fatal(v ...any)
  29. Panic(v ...any)
  30. }
  31. // FormatLogger is a logger interface that output logs with a format.
  32. type FormatLogger interface {
  33. Tracef(format string, v ...any)
  34. Debugf(format string, v ...any)
  35. Infof(format string, v ...any)
  36. Warnf(format string, v ...any)
  37. Errorf(format string, v ...any)
  38. Fatalf(format string, v ...any)
  39. Panicf(format string, v ...any)
  40. }
  41. // WithLogger is a logger interface that output logs with a message and key-value pairs.
  42. type WithLogger interface {
  43. Tracew(msg string, keysAndValues ...any)
  44. Debugw(msg string, keysAndValues ...any)
  45. Infow(msg string, keysAndValues ...any)
  46. Warnw(msg string, keysAndValues ...any)
  47. Errorw(msg string, keysAndValues ...any)
  48. Fatalw(msg string, keysAndValues ...any)
  49. Panicw(msg string, keysAndValues ...any)
  50. }
  51. // CommonLogger is the set of logging operations available across Fiber's
  52. // logging implementations.
  53. type CommonLogger interface {
  54. Logger
  55. FormatLogger
  56. WithLogger
  57. }
  58. // ConfigurableLogger provides methods to config a logger.
  59. type ConfigurableLogger[T any] interface {
  60. // SetLevel sets logging level.
  61. //
  62. // Available levels: Trace, Debug, Info, Warn, Error, Fatal, Panic.
  63. SetLevel(level Level)
  64. // SetOutput sets the logger output.
  65. SetOutput(w io.Writer)
  66. // Logger returns the logger instance. It can be used to adjust the logger configurations in case of need.
  67. Logger() T
  68. }
  69. // AllLogger is the combination of Logger, FormatLogger, CtxLogger and ConfigurableLogger.
  70. // Custom extensions can be made through AllLogger
  71. type AllLogger[T any] interface {
  72. CommonLogger
  73. ConfigurableLogger[T]
  74. // WithContext returns a new logger with the given context.
  75. WithContext(ctx context.Context) CommonLogger
  76. }
  77. // Level defines the priority of a log message.
  78. // When a logger is configured with a level, any log message with a lower
  79. // log level (smaller by integer comparison) will not be output.
  80. type Level int
  81. // The levels of logs.
  82. const (
  83. LevelTrace Level = iota
  84. LevelDebug
  85. LevelInfo
  86. LevelWarn
  87. LevelError
  88. LevelFatal
  89. LevelPanic
  90. )
  91. var strs = []string{
  92. "[Trace] ",
  93. "[Debug] ",
  94. "[Info] ",
  95. "[Warn] ",
  96. "[Error] ",
  97. "[Fatal] ",
  98. "[Panic] ",
  99. }
  100. func (lv Level) toString() string {
  101. if lv >= LevelTrace && lv <= LevelPanic {
  102. return strs[lv]
  103. }
  104. return fmt.Sprintf("[?%d] ", lv)
  105. }