contextlogger.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 &contextLogger{
  12. ctx: ctx,
  13. }
  14. }
  15. type contextLogger struct {
  16. logEntry
  17. ctx context.Context
  18. }
  19. func (l *contextLogger) Error(v ...interface{}) {
  20. l.err(fmt.Sprint(v...))
  21. }
  22. func (l *contextLogger) Errorf(format string, v ...interface{}) {
  23. l.err(fmt.Sprintf(format, v...))
  24. }
  25. func (l *contextLogger) Errorv(v interface{}) {
  26. l.err(fmt.Sprint(v))
  27. }
  28. func (l *contextLogger) Errorw(msg string, fields ...LogField) {
  29. l.err(msg, fields...)
  30. }
  31. func (l *contextLogger) Info(v ...interface{}) {
  32. l.info(fmt.Sprint(v...))
  33. }
  34. func (l *contextLogger) Infof(format string, v ...interface{}) {
  35. l.info(fmt.Sprintf(format, v...))
  36. }
  37. func (l *contextLogger) Infov(v interface{}) {
  38. l.info(v)
  39. }
  40. func (l *contextLogger) Infow(msg string, fields ...LogField) {
  41. l.info(msg, fields...)
  42. }
  43. func (l *contextLogger) Slow(v ...interface{}) {
  44. l.slow(fmt.Sprint(v...))
  45. }
  46. func (l *contextLogger) Slowf(format string, v ...interface{}) {
  47. l.slow(fmt.Sprintf(format, v...))
  48. }
  49. func (l *contextLogger) Slowv(v interface{}) {
  50. l.slow(v)
  51. }
  52. func (l *contextLogger) Sloww(msg string, fields ...LogField) {
  53. l.slow(msg, fields...)
  54. }
  55. func (l *contextLogger) WithContext(ctx context.Context) Logger {
  56. if ctx == nil {
  57. return l
  58. }
  59. l.ctx = ctx
  60. return l
  61. }
  62. func (l *contextLogger) WithDuration(duration time.Duration) Logger {
  63. l.Duration = timex.ReprOfDuration(duration)
  64. return l
  65. }
  66. func (l *contextLogger) 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. val := l.ctx.Value(fieldsContextKey)
  79. if val != nil {
  80. if arr, ok := val.([]LogField); ok {
  81. fields = append(fields, arr...)
  82. }
  83. }
  84. return fields
  85. }
  86. func (l *contextLogger) err(v interface{}, fields ...LogField) {
  87. if shallLog(ErrorLevel) {
  88. getWriter().Error(v, l.buildFields(fields...)...)
  89. }
  90. }
  91. func (l *contextLogger) info(v interface{}, fields ...LogField) {
  92. if shallLog(InfoLevel) {
  93. getWriter().Info(v, l.buildFields(fields...)...)
  94. }
  95. }
  96. func (l *contextLogger) slow(v interface{}, fields ...LogField) {
  97. if shallLog(ErrorLevel) {
  98. getWriter().Slow(v, l.buildFields(fields...)...)
  99. }
  100. }
  101. func spanIdFromContext(ctx context.Context) string {
  102. spanCtx := trace.SpanContextFromContext(ctx)
  103. if spanCtx.HasSpanID() {
  104. return spanCtx.SpanID().String()
  105. }
  106. return ""
  107. }
  108. func traceIdFromContext(ctx context.Context) string {
  109. spanCtx := trace.SpanContextFromContext(ctx)
  110. if spanCtx.HasTraceID() {
  111. return spanCtx.TraceID().String()
  112. }
  113. return ""
  114. }