logger.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package logger
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/gogf/gf/frame/g"
  6. "github.com/gogf/gf/os/glog"
  7. )
  8. // 定义键名
  9. const (
  10. TraceIDKey = "trace_id"
  11. UserIDKey = "user_id"
  12. SpanTitleKey = "span_title"
  13. SpanFunctionKey = "span_function"
  14. VersionKey = "version"
  15. )
  16. var log *glog.Logger
  17. // TraceIDFunc 定义获取跟踪ID的函数
  18. type TraceIDFunc func() string
  19. var (
  20. version string
  21. traceIDFunc TraceIDFunc
  22. )
  23. // SetTraceIdFunc 设置获取追踪Id的生成函数
  24. func SetTraceIdFunc(fn TraceIDFunc) {
  25. traceIDFunc = fn
  26. }
  27. // SetVersion 设置版本号
  28. func SetVersion(ver string) {
  29. version = ver
  30. }
  31. // FromTraceIDContext 从上下文中获取跟踪ID
  32. func FromTraceIDContext(ctx context.Context) string {
  33. v := ctx.Value(TraceIDKey)
  34. if v != nil {
  35. if s, ok := v.(string); ok {
  36. return s
  37. }
  38. }
  39. return getTraceID()
  40. }
  41. // NewUserIDContext 创建用户ID上下文
  42. func NewUserIDContext(ctx context.Context, userID string) context.Context {
  43. return context.WithValue(ctx, UserIDKey, userID)
  44. }
  45. // FromUserIDContext 从上下文中获取用户ID
  46. func FromUserIDContext(ctx context.Context) string {
  47. v := ctx.Value(UserIDKey)
  48. if v != nil {
  49. if s, ok := v.(string); ok {
  50. return s
  51. }
  52. }
  53. return ""
  54. }
  55. func getTraceID() string {
  56. if traceIDFunc != nil {
  57. return traceIDFunc()
  58. }
  59. return ""
  60. }
  61. // GetLogger 获取logger
  62. func GetLogger() *glog.Logger {
  63. return log
  64. }
  65. // NewTraceIDContext 创建跟踪ID上下文
  66. func NewTraceIDContext(ctx context.Context, traceID string) context.Context {
  67. return context.WithValue(ctx, TraceIDKey, traceID)
  68. }
  69. // Init 初始化日志配置
  70. func Init(runMode string) {
  71. if log != nil {
  72. panic(errors.New("重复初始化logger"))
  73. }
  74. log = g.Log()
  75. var lv string
  76. switch runMode {
  77. case "debug":
  78. lv = "DEV"
  79. case "test":
  80. lv = "DEV"
  81. case "release":
  82. lv = "PRODUCT"
  83. }
  84. err := log.SetLevelStr(lv)
  85. if err != nil {
  86. panic(err)
  87. }
  88. }
  89. type spanOptions struct {
  90. Title string
  91. FuncName string
  92. }
  93. // SpanOption 定义跟踪单元的数据项
  94. type SpanOption func(*spanOptions)
  95. // SetSpanTitle 设置跟踪单元的标题
  96. func SetSpanTitle(title string) SpanOption {
  97. return func(o *spanOptions) {
  98. o.Title = title
  99. }
  100. }
  101. // SetSpanFuncName 设置跟踪单元的函数名
  102. func SetSpanFuncName(funcName string) SpanOption {
  103. return func(o *spanOptions) {
  104. o.FuncName = funcName
  105. }
  106. }
  107. // StartSpan 开始一个追踪单元
  108. func StartSpan(ctx context.Context, opts ...SpanOption) *Entry {
  109. if ctx == nil {
  110. ctx = context.Background()
  111. }
  112. var o spanOptions
  113. for _, opt := range opts {
  114. opt(&o)
  115. }
  116. return NewEntry(log)
  117. }
  118. // Entry 定义统一的日志写入方式
  119. type Entry struct {
  120. entry *glog.Logger
  121. }
  122. func NewEntry(entry *glog.Logger) *Entry {
  123. return &Entry{entry: entry}
  124. }
  125. // Debugf 写入调试日志
  126. func Debugf(ctx context.Context, format string, args ...interface{}) {
  127. StartSpan(ctx).entry.Ctx(ctx).Debugf(format, args)
  128. }
  129. // Printf 写入消息日志
  130. func Printf(ctx context.Context, format string, args ...interface{}) {
  131. StartSpan(ctx).entry.Ctx(ctx).Printf(format, args...)
  132. }
  133. // Warnf 写入警告日志
  134. func Warnf(ctx context.Context, format string, args ...interface{}) {
  135. StartSpan(ctx).entry.Ctx(ctx).Warningf(format, args...)
  136. }
  137. // Fatalf 写入重大错误日志
  138. func Fatalf(ctx context.Context, format string, args ...interface{}) {
  139. StartSpan(ctx).entry.Ctx(ctx).Fatalf(format, args...)
  140. }
  141. // Errorf 错误日志
  142. func Errorf(ctx context.Context, format string, args ...interface{}) {
  143. StartSpan(ctx).entry.Ctx(ctx).Errorf(format, args...)
  144. }
  145. // Errorf 错误日志
  146. func (e *Entry) Errorf(format string, args ...interface{}) {
  147. e.entry.Errorf(format, args...)
  148. }
  149. // Warnf 警告日志
  150. func (e *Entry) Warnf(format string, args ...interface{}) {
  151. e.entry.Warningf(format, args...)
  152. }
  153. // Infof 消息日志
  154. func (e *Entry) Infof(format string, args ...interface{}) {
  155. e.entry.Infof(format, args...)
  156. }
  157. // Printf 消息日志
  158. func (e *Entry) Printf(format string, args ...interface{}) {
  159. e.entry.Printf(format, args...)
  160. }
  161. // Debugf 写入调试日志
  162. func (e *Entry) Debugf(format string, args ...interface{}) {
  163. e.entry.Debugf(format, args...)
  164. }