1
0

richlogger.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package logx
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/wuntsong-org/go-zero-plus/core/timex"
  7. "github.com/wuntsong-org/go-zero-plus/internal/trace"
  8. )
  9. // WithCallerSkip returns a Logger with given caller skip.
  10. func WithCallerSkip(skip int) Logger {
  11. if skip <= 0 {
  12. return new(richLogger)
  13. }
  14. return &richLogger{
  15. callerSkip: skip,
  16. }
  17. }
  18. // WithContext sets ctx to log, for keeping tracing information.
  19. func WithContext(ctx context.Context) Logger {
  20. return &richLogger{
  21. ctx: ctx,
  22. }
  23. }
  24. // WithDuration returns a Logger with given duration.
  25. func WithDuration(d time.Duration) Logger {
  26. return &richLogger{
  27. fields: []LogField{Field(durationKey, timex.ReprOfDuration(d))},
  28. }
  29. }
  30. type richLogger struct {
  31. ctx context.Context
  32. callerSkip int
  33. fields []LogField
  34. }
  35. func (l *richLogger) Debug(v ...any) {
  36. if shallLog(DebugLevel) {
  37. l.debug(fmt.Sprint(v...))
  38. }
  39. }
  40. func (l *richLogger) Debugf(format string, v ...any) {
  41. if shallLog(DebugLevel) {
  42. l.debug(fmt.Sprintf(format, v...))
  43. }
  44. }
  45. func (l *richLogger) Debugv(v any) {
  46. if shallLog(DebugLevel) {
  47. l.debug(v)
  48. }
  49. }
  50. func (l *richLogger) Debugw(msg string, fields ...LogField) {
  51. if shallLog(DebugLevel) {
  52. l.debug(msg, fields...)
  53. }
  54. }
  55. func (l *richLogger) Error(v ...any) {
  56. if shallLog(ErrorLevel) {
  57. l.err(fmt.Sprint(v...))
  58. }
  59. }
  60. func (l *richLogger) Errorf(format string, v ...any) {
  61. if shallLog(ErrorLevel) {
  62. l.err(fmt.Sprintf(format, v...))
  63. }
  64. }
  65. func (l *richLogger) Errorv(v any) {
  66. if shallLog(ErrorLevel) {
  67. l.err(v)
  68. }
  69. }
  70. func (l *richLogger) Errorw(msg string, fields ...LogField) {
  71. if shallLog(ErrorLevel) {
  72. l.err(msg, fields...)
  73. }
  74. }
  75. func (l *richLogger) Info(v ...any) {
  76. if shallLog(InfoLevel) {
  77. l.info(fmt.Sprint(v...))
  78. }
  79. }
  80. func (l *richLogger) Infof(format string, v ...any) {
  81. if shallLog(InfoLevel) {
  82. l.info(fmt.Sprintf(format, v...))
  83. }
  84. }
  85. func (l *richLogger) Infov(v any) {
  86. if shallLog(InfoLevel) {
  87. l.info(v)
  88. }
  89. }
  90. func (l *richLogger) Infow(msg string, fields ...LogField) {
  91. if shallLog(InfoLevel) {
  92. l.info(msg, fields...)
  93. }
  94. }
  95. func (l *richLogger) Slow(v ...any) {
  96. if shallLog(ErrorLevel) {
  97. l.slow(fmt.Sprint(v...))
  98. }
  99. }
  100. func (l *richLogger) Slowf(format string, v ...any) {
  101. if shallLog(ErrorLevel) {
  102. l.slow(fmt.Sprintf(format, v...))
  103. }
  104. }
  105. func (l *richLogger) Slowv(v any) {
  106. if shallLog(ErrorLevel) {
  107. l.slow(v)
  108. }
  109. }
  110. func (l *richLogger) Sloww(msg string, fields ...LogField) {
  111. if shallLog(ErrorLevel) {
  112. l.slow(msg, fields...)
  113. }
  114. }
  115. func (l *richLogger) WithCallerSkip(skip int) Logger {
  116. if skip <= 0 {
  117. return l
  118. }
  119. l.callerSkip = skip
  120. return l
  121. }
  122. func (l *richLogger) WithContext(ctx context.Context) Logger {
  123. l.ctx = ctx
  124. return l
  125. }
  126. func (l *richLogger) WithDuration(duration time.Duration) Logger {
  127. l.fields = append(l.fields, Field(durationKey, timex.ReprOfDuration(duration)))
  128. return l
  129. }
  130. func (l *richLogger) WithFields(fields ...LogField) Logger {
  131. l.fields = append(l.fields, fields...)
  132. return l
  133. }
  134. func (l *richLogger) buildFields(fields ...LogField) []LogField {
  135. fields = append(l.fields, fields...)
  136. fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip)))
  137. if l.ctx == nil {
  138. return fields
  139. }
  140. traceID := trace.TraceIDFromContext(l.ctx)
  141. if len(traceID) > 0 {
  142. fields = append(fields, Field(traceKey, traceID))
  143. }
  144. spanID := trace.SpanIDFromContext(l.ctx)
  145. if len(spanID) > 0 {
  146. fields = append(fields, Field(spanKey, spanID))
  147. }
  148. val := l.ctx.Value(fieldsContextKey)
  149. if val != nil {
  150. if arr, ok := val.([]LogField); ok {
  151. fields = append(fields, arr...)
  152. }
  153. }
  154. return fields
  155. }
  156. func (l *richLogger) debug(v any, fields ...LogField) {
  157. if shallLog(DebugLevel) {
  158. getWriter().Debug(v, l.buildFields(fields...)...)
  159. }
  160. }
  161. func (l *richLogger) err(v any, fields ...LogField) {
  162. if shallLog(ErrorLevel) {
  163. getWriter().Error(v, l.buildFields(fields...)...)
  164. }
  165. }
  166. func (l *richLogger) info(v any, fields ...LogField) {
  167. if shallLog(InfoLevel) {
  168. getWriter().Info(v, l.buildFields(fields...)...)
  169. }
  170. }
  171. func (l *richLogger) slow(v any, fields ...LogField) {
  172. if shallLog(ErrorLevel) {
  173. getWriter().Slow(v, l.buildFields(fields...)...)
  174. }
  175. }