format.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package log
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "time"
  7. F "github.com/sagernet/sing/common/format"
  8. "github.com/logrusorgru/aurora"
  9. )
  10. type Formatter struct {
  11. BaseTime time.Time
  12. DisableColors bool
  13. DisableTimestamp bool
  14. FullTimestamp bool
  15. TimestampFormat string
  16. }
  17. func (f Formatter) Format(ctx context.Context, level Level, tag string, message string, timestamp time.Time) string {
  18. levelString := strings.ToUpper(FormatLevel(level))
  19. if !f.DisableColors {
  20. switch level {
  21. case LevelDebug, LevelTrace:
  22. levelString = aurora.White(levelString).String()
  23. case LevelInfo:
  24. levelString = aurora.Cyan(levelString).String()
  25. case LevelWarn:
  26. levelString = aurora.Yellow(levelString).String()
  27. case LevelError, LevelFatal, LevelPanic:
  28. levelString = aurora.Red(levelString).String()
  29. }
  30. }
  31. if tag != "" {
  32. message = tag + ": " + message
  33. }
  34. var id uint32
  35. var hasId bool
  36. if ctx != nil {
  37. id, hasId = IDFromContext(ctx)
  38. }
  39. if hasId {
  40. if !f.DisableColors {
  41. var color aurora.Color
  42. color = aurora.Color(uint8(id))
  43. color %= 215
  44. row := uint(color / 36)
  45. column := uint(color % 36)
  46. var r, g, b float32
  47. r = float32(row * 51)
  48. g = float32(column / 6 * 51)
  49. b = float32((column % 6) * 51)
  50. luma := 0.2126*r + 0.7152*g + 0.0722*b
  51. if luma < 60 {
  52. row = 5 - row
  53. column = 35 - column
  54. color = aurora.Color(row*36 + column)
  55. }
  56. color += 16
  57. color = color << 16
  58. color |= 1 << 14
  59. message = F.ToString("[", aurora.Colorize(id, color).String(), "] ", message)
  60. } else {
  61. message = F.ToString("[", id, "] ", message)
  62. }
  63. }
  64. switch {
  65. case f.DisableTimestamp:
  66. message = levelString + " " + message
  67. case f.FullTimestamp:
  68. message = timestamp.Format(f.TimestampFormat) + " " + levelString + " " + message
  69. default:
  70. message = levelString + "[" + xd(int(timestamp.Sub(f.BaseTime)/time.Second), 4) + "] " + message
  71. }
  72. if message[len(message)-1] != '\n' {
  73. message += "\n"
  74. }
  75. return message
  76. }
  77. func (f Formatter) FormatWithSimple(ctx context.Context, level Level, tag string, message string, timestamp time.Time) (string, string) {
  78. levelString := strings.ToUpper(FormatLevel(level))
  79. if !f.DisableColors {
  80. switch level {
  81. case LevelDebug, LevelTrace:
  82. levelString = aurora.White(levelString).String()
  83. case LevelInfo:
  84. levelString = aurora.Cyan(levelString).String()
  85. case LevelWarn:
  86. levelString = aurora.Yellow(levelString).String()
  87. case LevelError, LevelFatal, LevelPanic:
  88. levelString = aurora.Red(levelString).String()
  89. }
  90. }
  91. if tag != "" {
  92. message = tag + ": " + message
  93. }
  94. messageSimple := message
  95. var id uint32
  96. var hasId bool
  97. if ctx != nil {
  98. id, hasId = IDFromContext(ctx)
  99. }
  100. if hasId {
  101. if !f.DisableColors {
  102. var color aurora.Color
  103. color = aurora.Color(uint8(id))
  104. color %= 215
  105. row := uint(color / 36)
  106. column := uint(color % 36)
  107. var r, g, b float32
  108. r = float32(row * 51)
  109. g = float32(column / 6 * 51)
  110. b = float32((column % 6) * 51)
  111. luma := 0.2126*r + 0.7152*g + 0.0722*b
  112. if luma < 60 {
  113. row = 5 - row
  114. column = 35 - column
  115. color = aurora.Color(row*36 + column)
  116. }
  117. color += 16
  118. color = color << 16
  119. color |= 1 << 14
  120. message = F.ToString("[", aurora.Colorize(id, color).String(), "] ", message)
  121. } else {
  122. message = F.ToString("[", id, "] ", message)
  123. }
  124. messageSimple = F.ToString("[", id, "] ", messageSimple)
  125. }
  126. switch {
  127. case f.DisableTimestamp:
  128. message = levelString + " " + message
  129. case f.FullTimestamp:
  130. message = timestamp.Format(f.TimestampFormat) + " " + levelString + " " + message
  131. default:
  132. message = levelString + "[" + xd(int(timestamp.Sub(f.BaseTime)/time.Second), 4) + "] " + message
  133. }
  134. if message[len(message)-1] != '\n' {
  135. message += "\n"
  136. }
  137. return message, messageSimple
  138. }
  139. func xd(value int, x int) string {
  140. message := strconv.Itoa(value)
  141. for len(message) < x {
  142. message = "0" + message
  143. }
  144. return message
  145. }