tracelogger.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package logx
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/tal-tech/go-zero/core/timex"
  8. "github.com/tal-tech/go-zero/core/trace/tracespec"
  9. )
  10. type traceLogger struct {
  11. logEntry
  12. Trace string `json:"trace,omitempty"`
  13. Span string `json:"span,omitempty"`
  14. ctx context.Context
  15. }
  16. func (l *traceLogger) Error(v ...interface{}) {
  17. if shallLog(ErrorLevel) {
  18. l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
  19. }
  20. }
  21. func (l *traceLogger) Errorf(format string, v ...interface{}) {
  22. if shallLog(ErrorLevel) {
  23. l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
  24. }
  25. }
  26. func (l *traceLogger) Errorv(v interface{}) {
  27. if shallLog(ErrorLevel) {
  28. l.write(errorLog, levelError, v)
  29. }
  30. }
  31. func (l *traceLogger) Info(v ...interface{}) {
  32. if shallLog(InfoLevel) {
  33. l.write(infoLog, levelInfo, fmt.Sprint(v...))
  34. }
  35. }
  36. func (l *traceLogger) Infof(format string, v ...interface{}) {
  37. if shallLog(InfoLevel) {
  38. l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
  39. }
  40. }
  41. func (l *traceLogger) Infov(v interface{}) {
  42. if shallLog(InfoLevel) {
  43. l.write(infoLog, levelInfo, v)
  44. }
  45. }
  46. func (l *traceLogger) Slow(v ...interface{}) {
  47. if shallLog(ErrorLevel) {
  48. l.write(slowLog, levelSlow, fmt.Sprint(v...))
  49. }
  50. }
  51. func (l *traceLogger) Slowf(format string, v ...interface{}) {
  52. if shallLog(ErrorLevel) {
  53. l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
  54. }
  55. }
  56. func (l *traceLogger) Slowv(v interface{}) {
  57. if shallLog(ErrorLevel) {
  58. l.write(slowLog, levelSlow, v)
  59. }
  60. }
  61. func (l *traceLogger) WithDuration(duration time.Duration) Logger {
  62. l.Duration = timex.ReprOfDuration(duration)
  63. return l
  64. }
  65. func (l *traceLogger) write(writer io.Writer, level string, val interface{}) {
  66. l.Timestamp = getTimestamp()
  67. l.Level = level
  68. l.Content = val
  69. l.Trace = traceIdFromContext(l.ctx)
  70. l.Span = spanIdFromContext(l.ctx)
  71. outputJson(writer, l)
  72. }
  73. // WithContext sets ctx to log, for keeping tracing information.
  74. func WithContext(ctx context.Context) Logger {
  75. return &traceLogger{
  76. ctx: ctx,
  77. }
  78. }
  79. func spanIdFromContext(ctx context.Context) string {
  80. t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
  81. if !ok {
  82. return ""
  83. }
  84. return t.SpanId()
  85. }
  86. func traceIdFromContext(ctx context.Context) string {
  87. t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
  88. if !ok {
  89. return ""
  90. }
  91. return t.TraceId()
  92. }