log.go 2.3 KB

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