logger.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package logger
  2. import (
  3. "fmt"
  4. "os"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. )
  11. const TimeFormatDay = "2006-01-02"
  12. const TimeFormat = "2006-01-02 15:04:05"
  13. var currentLogs = make(map[string]string)
  14. func makeString(message ...string) string {
  15. return strings.Join(message, " ")
  16. }
  17. func getVerbose() int32 {
  18. level, err := strconv.Atoi(os.Getenv("VERBOSITY"))
  19. if err != nil || level < 0 {
  20. level = 0
  21. }
  22. if level > 3 {
  23. level = 3
  24. }
  25. return int32(level)
  26. }
  27. // ResetLogs - reallocates logs map
  28. func ResetLogs() {
  29. currentLogs = make(map[string]string)
  30. }
  31. // Log - handles adding logs
  32. func Log(verbosity int, message ...string) {
  33. var mu sync.Mutex
  34. mu.Lock()
  35. defer mu.Unlock()
  36. var currentTime = time.Now()
  37. var currentMessage = makeString(message...)
  38. if int32(verbosity) <= getVerbose() && getVerbose() >= 0 {
  39. fmt.Printf("[netmaker] %s %s \n", currentTime.Format(TimeFormat), currentMessage)
  40. }
  41. currentLogs[currentMessage] = currentTime.Format("2006-01-02 15:04:05.999999999")
  42. }
  43. // Dump - dumps all logs into a formatted string
  44. func Dump() string {
  45. var dumpString = ""
  46. type keyVal struct {
  47. Key string
  48. Value time.Time
  49. }
  50. var mu sync.Mutex
  51. mu.Lock()
  52. defer mu.Unlock()
  53. var dumpLogs = make([]keyVal, 0, len(currentLogs))
  54. for key, value := range currentLogs {
  55. parsedTime, err := time.Parse(TimeFormat, value)
  56. if err == nil {
  57. dumpLogs = append(dumpLogs, keyVal{
  58. Key: key,
  59. Value: parsedTime,
  60. })
  61. }
  62. }
  63. sort.Slice(dumpLogs, func(i, j int) bool {
  64. return dumpLogs[i].Value.Before(dumpLogs[j].Value)
  65. })
  66. for i := range dumpLogs {
  67. var currLog = dumpLogs[i]
  68. dumpString += fmt.Sprintf("[netmaker] %s %s \n", currLog.Value.Format(TimeFormat), currLog.Key)
  69. }
  70. return dumpString
  71. }
  72. // DumpFile - appends log dump log file
  73. func DumpFile(filePath string) {
  74. f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
  75. if err != nil {
  76. panic(err)
  77. }
  78. defer f.Close()
  79. if _, err = f.WriteString(Dump()); err != nil {
  80. panic(err)
  81. }
  82. }
  83. // Retrieve - retrieves logs from given file
  84. func Retrieve(filePath string) string {
  85. contents, err := os.ReadFile(filePath)
  86. if err != nil {
  87. panic(err)
  88. }
  89. return string(contents)
  90. }
  91. // FatalLog - exits os after logging
  92. func FatalLog(message ...string) {
  93. var mu sync.Mutex
  94. mu.Lock()
  95. defer mu.Unlock()
  96. fmt.Printf("[netmaker] Fatal: %s \n", makeString(message...))
  97. os.Exit(2)
  98. }