logger.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. break
  80. case "test":
  81. lv = "DEV"
  82. break
  83. case "release":
  84. lv = "PRODUCT"
  85. default:
  86. lv = "DEV"
  87. break
  88. }
  89. err := log.SetLevelStr(lv)
  90. if err != nil {
  91. panic(err)
  92. }
  93. }
  94. type spanOptions struct {
  95. Title string
  96. FuncName string
  97. }
  98. // SpanOption 定义跟踪单元的数据项
  99. type SpanOption func(*spanOptions)
  100. // SetSpanTitle 设置跟踪单元的标题
  101. func SetSpanTitle(title string) SpanOption {
  102. return func(o *spanOptions) {
  103. o.Title = title
  104. }
  105. }
  106. // SetSpanFuncName 设置跟踪单元的函数名
  107. func SetSpanFuncName(funcName string) SpanOption {
  108. return func(o *spanOptions) {
  109. o.FuncName = funcName
  110. }
  111. }
  112. // StartSpan 开始一个追踪单元
  113. func StartSpan(ctx context.Context, opts ...SpanOption) *Entry {
  114. if ctx == nil {
  115. ctx = context.Background()
  116. }
  117. var o spanOptions
  118. for _, opt := range opts {
  119. opt(&o)
  120. }
  121. return NewEntry(log)
  122. }
  123. // Entry 定义统一的日志写入方式
  124. type Entry struct {
  125. entry *glog.Logger
  126. }
  127. func NewEntry(entry *glog.Logger) *Entry {
  128. return &Entry{entry: entry}
  129. }
  130. // Debugf 写入调试日志
  131. func Debugf(ctx context.Context, format string, args ...interface{}) {
  132. StartSpan(ctx).entry.Ctx(ctx).Debugf(format, args)
  133. }
  134. // Printf 写入消息日志
  135. func Printf(ctx context.Context, format string, args ...interface{}) {
  136. StartSpan(ctx).entry.Ctx(ctx).Printf(format, args...)
  137. }
  138. // Warnf 写入警告日志
  139. func Warnf(ctx context.Context, format string, args ...interface{}) {
  140. StartSpan(ctx).entry.Ctx(ctx).Warningf(format, args...)
  141. }
  142. // Fatalf 写入重大错误日志
  143. func Fatalf(ctx context.Context, format string, args ...interface{}) {
  144. StartSpan(ctx).entry.Ctx(ctx).Fatalf(format, args...)
  145. }
  146. // Errorf 错误日志
  147. func Errorf(ctx context.Context, format string, args ...interface{}) {
  148. StartSpan(ctx).entry.Ctx(ctx).Errorf(format, args...)
  149. }
  150. // Errorf 错误日志
  151. func (e *Entry) Errorf(format string, args ...interface{}) {
  152. e.entry.Errorf(format, args...)
  153. }
  154. // Warnf 警告日志
  155. func (e *Entry) Warnf(format string, args ...interface{}) {
  156. e.entry.Warningf(format, args...)
  157. }
  158. // Infof 消息日志
  159. func (e *Entry) Infof(format string, args ...interface{}) {
  160. e.entry.Infof(format, args...)
  161. }
  162. // Printf 消息日志
  163. func (e *Entry) Printf(format string, args ...interface{}) {
  164. e.entry.Printf(format, args...)
  165. }
  166. // Debugf 写入调试日志
  167. func (e *Entry) Debugf(format string, args ...interface{}) {
  168. e.entry.Debugf(format, args...)
  169. }