tracelogger.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package logx
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/zeromicro/go-zero/core/timex"
  7. "go.opentelemetry.io/otel/trace"
  8. )
  9. // WithContext sets ctx to log, for keeping tracing information.
  10. func WithContext(ctx context.Context) Logger {
  11. return &traceLogger{
  12. ctx: ctx,
  13. }
  14. }
  15. type traceLogger struct {
  16. logEntry
  17. ctx context.Context
  18. }
  19. func (l *traceLogger) Error(v ...interface{}) {
  20. l.err(fmt.Sprint(v...))
  21. }
  22. func (l *traceLogger) Errorf(format string, v ...interface{}) {
  23. l.err(fmt.Sprintf(format, v...))
  24. }
  25. func (l *traceLogger) Errorv(v interface{}) {
  26. l.err(fmt.Sprint(v))
  27. }
  28. func (l *traceLogger) Errorw(msg string, fields ...LogField) {
  29. l.err(msg, fields...)
  30. }
  31. func (l *traceLogger) Info(v ...interface{}) {
  32. l.info(fmt.Sprint(v...))
  33. }
  34. func (l *traceLogger) Infof(format string, v ...interface{}) {
  35. l.info(fmt.Sprintf(format, v...))
  36. }
  37. func (l *traceLogger) Infov(v interface{}) {
  38. l.info(v)
  39. }
  40. func (l *traceLogger) Infow(msg string, fields ...LogField) {
  41. l.info(msg, fields...)
  42. }
  43. func (l *traceLogger) Slow(v ...interface{}) {
  44. l.slow(fmt.Sprint(v...))
  45. }
  46. func (l *traceLogger) Slowf(format string, v ...interface{}) {
  47. l.slow(fmt.Sprintf(format, v...))
  48. }
  49. func (l *traceLogger) Slowv(v interface{}) {
  50. l.slow(v)
  51. }
  52. func (l *traceLogger) Sloww(msg string, fields ...LogField) {
  53. l.slow(msg, fields...)
  54. }
  55. func (l *traceLogger) WithContext(ctx context.Context) Logger {
  56. if ctx == nil {
  57. return l
  58. }
  59. l.ctx = ctx
  60. return l
  61. }
  62. func (l *traceLogger) WithDuration(duration time.Duration) Logger {
  63. l.Duration = timex.ReprOfDuration(duration)
  64. return l
  65. }
  66. func (l *traceLogger) buildFields(fields ...LogField) []LogField {
  67. if len(l.Duration) > 0 {
  68. fields = append(fields, Field(durationKey, l.Duration))
  69. }
  70. traceID := traceIdFromContext(l.ctx)
  71. if len(traceID) > 0 {
  72. fields = append(fields, Field(traceKey, traceID))
  73. }
  74. spanID := spanIdFromContext(l.ctx)
  75. if len(spanID) > 0 {
  76. fields = append(fields, Field(spanKey, spanID))
  77. }
  78. return fields
  79. }
  80. func (l *traceLogger) err(v interface{}, fields ...LogField) {
  81. if shallLog(ErrorLevel) {
  82. getWriter().Error(v, l.buildFields(fields...)...)
  83. }
  84. }
  85. func (l *traceLogger) info(v interface{}, fields ...LogField) {
  86. if shallLog(InfoLevel) {
  87. getWriter().Info(v, l.buildFields(fields...)...)
  88. }
  89. }
  90. func (l *traceLogger) slow(v interface{}, fields ...LogField) {
  91. if shallLog(ErrorLevel) {
  92. getWriter().Slow(v, l.buildFields(fields...)...)
  93. }
  94. }
  95. func spanIdFromContext(ctx context.Context) string {
  96. spanCtx := trace.SpanContextFromContext(ctx)
  97. if spanCtx.HasSpanID() {
  98. return spanCtx.SpanID().String()
  99. }
  100. return ""
  101. }
  102. func traceIdFromContext(ctx context.Context) string {
  103. spanCtx := trace.SpanContextFromContext(ctx)
  104. if spanCtx.HasTraceID() {
  105. return spanCtx.TraceID().String()
  106. }
  107. return ""
  108. }