level.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package logger
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. type Level int8
  7. const (
  8. // TraceLevel level. Designates finer-grained informational events than the Debug.
  9. TraceLevel Level = iota - 2
  10. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  11. DebugLevel
  12. // InfoLevel is the default logging priority.
  13. // General operational entries about what's going on inside the application.
  14. InfoLevel
  15. // WarnLevel level. Non-critical entries that deserve eyes.
  16. WarnLevel
  17. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  18. ErrorLevel
  19. // FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity.
  20. FatalLevel
  21. )
  22. func (l Level) String() string {
  23. switch l {
  24. case TraceLevel:
  25. return "trace"
  26. case DebugLevel:
  27. return "debug"
  28. case InfoLevel:
  29. return "info"
  30. case WarnLevel:
  31. return "warn"
  32. case ErrorLevel:
  33. return "error"
  34. case FatalLevel:
  35. return "fatal"
  36. }
  37. return ""
  38. }
  39. // LevelForGorm 转换成gorm日志级别
  40. func (l Level) LevelForGorm() int {
  41. switch l {
  42. case FatalLevel, ErrorLevel:
  43. return 2
  44. case WarnLevel:
  45. return 3
  46. case InfoLevel, DebugLevel, TraceLevel:
  47. return 4
  48. default:
  49. return 1
  50. }
  51. }
  52. // Enabled returns true if the given level is at or above this level.
  53. func (l Level) Enabled(lvl Level) bool {
  54. return lvl >= l
  55. }
  56. // GetLevel converts a level string into a logger Level value.
  57. // returns an error if the input string does not match known values.
  58. func GetLevel(levelStr string) (Level, error) {
  59. switch levelStr {
  60. case TraceLevel.String():
  61. return TraceLevel, nil
  62. case DebugLevel.String():
  63. return DebugLevel, nil
  64. case InfoLevel.String():
  65. return InfoLevel, nil
  66. case WarnLevel.String():
  67. return WarnLevel, nil
  68. case ErrorLevel.String():
  69. return ErrorLevel, nil
  70. case FatalLevel.String():
  71. return FatalLevel, nil
  72. }
  73. return InfoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to InfoLevel", levelStr)
  74. }
  75. func Info(args ...interface{}) {
  76. DefaultLogger.Log(InfoLevel, args...)
  77. }
  78. func Infof(template string, args ...interface{}) {
  79. DefaultLogger.Logf(InfoLevel, template, args...)
  80. }
  81. func Trace(args ...interface{}) {
  82. DefaultLogger.Log(TraceLevel, args...)
  83. }
  84. func Tracef(template string, args ...interface{}) {
  85. DefaultLogger.Logf(TraceLevel, template, args...)
  86. }
  87. func Debug(args ...interface{}) {
  88. DefaultLogger.Log(DebugLevel, args...)
  89. }
  90. func Debugf(template string, args ...interface{}) {
  91. DefaultLogger.Logf(DebugLevel, template, args...)
  92. }
  93. func Warn(args ...interface{}) {
  94. DefaultLogger.Log(WarnLevel, args...)
  95. }
  96. func Warnf(template string, args ...interface{}) {
  97. DefaultLogger.Logf(WarnLevel, template, args...)
  98. }
  99. func Error(args ...interface{}) {
  100. DefaultLogger.Log(ErrorLevel, args...)
  101. }
  102. func Errorf(template string, args ...interface{}) {
  103. DefaultLogger.Logf(ErrorLevel, template, args...)
  104. }
  105. func Fatal(args ...interface{}) {
  106. DefaultLogger.Log(FatalLevel, args...)
  107. os.Exit(1)
  108. }
  109. func Fatalf(template string, args ...interface{}) {
  110. DefaultLogger.Logf(FatalLevel, template, args...)
  111. os.Exit(1)
  112. }
  113. // Returns true if the given level is at or lower the current logger level
  114. func V(lvl Level, log Logger) bool {
  115. l := DefaultLogger
  116. if log != nil {
  117. l = log
  118. }
  119. return l.Options().Level <= lvl
  120. }