log.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package internal
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "sync"
  8. "github.com/wuntsong-org/go-zero-plus/core/logx"
  9. "github.com/wuntsong-org/go-zero-plus/rest/httpx"
  10. )
  11. // logContextKey is a context key.
  12. var logContextKey = contextKey("request_logs")
  13. type (
  14. // LogCollector is used to collect logs.
  15. LogCollector struct {
  16. Messages []string
  17. lock sync.Mutex
  18. }
  19. contextKey string
  20. )
  21. // WithLogCollector returns a new context with LogCollector.
  22. func WithLogCollector(ctx context.Context, lc *LogCollector) context.Context {
  23. return context.WithValue(ctx, logContextKey, lc)
  24. }
  25. // LogCollectorFromContext returns LogCollector from ctx.
  26. func LogCollectorFromContext(ctx context.Context) *LogCollector {
  27. val := ctx.Value(logContextKey)
  28. if val == nil {
  29. return nil
  30. }
  31. return val.(*LogCollector)
  32. }
  33. // Append appends msg into log context.
  34. func (lc *LogCollector) Append(msg string) {
  35. lc.lock.Lock()
  36. lc.Messages = append(lc.Messages, msg)
  37. lc.lock.Unlock()
  38. }
  39. // Flush flushes collected logs.
  40. func (lc *LogCollector) Flush() string {
  41. var buffer bytes.Buffer
  42. start := true
  43. for _, message := range lc.takeAll() {
  44. if start {
  45. start = false
  46. } else {
  47. buffer.WriteByte('\n')
  48. }
  49. buffer.WriteString(message)
  50. }
  51. return buffer.String()
  52. }
  53. func (lc *LogCollector) takeAll() []string {
  54. lc.lock.Lock()
  55. messages := lc.Messages
  56. lc.Messages = nil
  57. lc.lock.Unlock()
  58. return messages
  59. }
  60. // Error logs the given v along with r in error log.
  61. func Error(r *http.Request, v ...any) {
  62. logx.WithContext(r.Context()).Error(format(r, v...))
  63. }
  64. // Errorf logs the given v with format along with r in error log.
  65. func Errorf(r *http.Request, format string, v ...any) {
  66. logx.WithContext(r.Context()).Error(formatf(r, format, v...))
  67. }
  68. // Info logs the given v along with r in access log.
  69. func Info(r *http.Request, v ...any) {
  70. appendLog(r, format(r, v...))
  71. }
  72. // Infof logs the given v with format along with r in access log.
  73. func Infof(r *http.Request, format string, v ...any) {
  74. appendLog(r, formatf(r, format, v...))
  75. }
  76. func appendLog(r *http.Request, message string) {
  77. logs := LogCollectorFromContext(r.Context())
  78. if logs != nil {
  79. logs.Append(message)
  80. }
  81. }
  82. func format(r *http.Request, v ...any) string {
  83. return formatWithReq(r, fmt.Sprint(v...))
  84. }
  85. func formatf(r *http.Request, format string, v ...any) string {
  86. return formatWithReq(r, fmt.Sprintf(format, v...))
  87. }
  88. func formatWithReq(r *http.Request, v string) string {
  89. return fmt.Sprintf("(%s - %s) %s", r.RequestURI, httpx.GetRemoteAddr(r), v)
  90. }