log.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package log
  2. import (
  3. "log"
  4. "strings"
  5. "sync"
  6. )
  7. // Level is a well-known log level, as defined below
  8. type Level int
  9. // Well known log levels
  10. const (
  11. TraceLevel Level = iota
  12. DebugLevel
  13. InfoLevel
  14. WarnLevel
  15. ErrorLevel
  16. )
  17. func (l Level) String() string {
  18. switch l {
  19. case TraceLevel:
  20. return "TRACE"
  21. case DebugLevel:
  22. return "DEBUG"
  23. case InfoLevel:
  24. return "INFO"
  25. case WarnLevel:
  26. return "WARN"
  27. case ErrorLevel:
  28. return "ERROR"
  29. }
  30. return "unknown"
  31. }
  32. var (
  33. level = InfoLevel
  34. mu = &sync.Mutex{}
  35. )
  36. // Trace prints the given message, if the current log level is TRACE
  37. func Trace(message string, v ...any) {
  38. logIf(TraceLevel, message, v...)
  39. }
  40. // Debug prints the given message, if the current log level is DEBUG or lower
  41. func Debug(message string, v ...any) {
  42. logIf(DebugLevel, message, v...)
  43. }
  44. // Info prints the given message, if the current log level is INFO or lower
  45. func Info(message string, v ...any) {
  46. logIf(InfoLevel, message, v...)
  47. }
  48. // Warn prints the given message, if the current log level is WARN or lower
  49. func Warn(message string, v ...any) {
  50. logIf(WarnLevel, message, v...)
  51. }
  52. // Error prints the given message, if the current log level is ERROR or lower
  53. func Error(message string, v ...any) {
  54. logIf(ErrorLevel, message, v...)
  55. }
  56. // Fatal prints the given message, and exits the program
  57. func Fatal(v ...any) {
  58. log.Fatalln(v...)
  59. }
  60. // CurrentLevel returns the current log level
  61. func CurrentLevel() Level {
  62. mu.Lock()
  63. defer mu.Unlock()
  64. return level
  65. }
  66. // SetLevel sets a new log level
  67. func SetLevel(newLevel Level) {
  68. mu.Lock()
  69. defer mu.Unlock()
  70. level = newLevel
  71. }
  72. // DisableDates disables the date/time prefix
  73. func DisableDates() {
  74. log.SetFlags(0)
  75. }
  76. // ToLevel converts a string to a Level. It returns InfoLevel if the string
  77. // does not match any known log levels.
  78. func ToLevel(s string) Level {
  79. switch strings.ToUpper(s) {
  80. case "TRACE":
  81. return TraceLevel
  82. case "DEBUG":
  83. return DebugLevel
  84. case "INFO":
  85. return InfoLevel
  86. case "WARN", "WARNING":
  87. return WarnLevel
  88. case "ERROR":
  89. return ErrorLevel
  90. default:
  91. return InfoLevel
  92. }
  93. }
  94. // Loggable returns true if the given log level is lower or equal to the current log level
  95. func Loggable(l Level) bool {
  96. return CurrentLevel() <= l
  97. }
  98. // IsTrace returns true if the current log level is TraceLevel
  99. func IsTrace() bool {
  100. return Loggable(TraceLevel)
  101. }
  102. // IsDebug returns true if the current log level is DebugLevel or below
  103. func IsDebug() bool {
  104. return Loggable(DebugLevel)
  105. }
  106. func logIf(l Level, message string, v ...any) {
  107. if CurrentLevel() <= l {
  108. log.Printf(l.String()+" "+message, v...)
  109. }
  110. }