glog_ctx.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package glog
  2. import (
  3. "context"
  4. "fmt"
  5. "sync/atomic"
  6. reqid "github.com/seaweedfs/seaweedfs/weed/util/request_id"
  7. )
  8. const requestIDField = "request_id"
  9. // formatMetaTag returns a formatted request ID tag from the context,
  10. // like "request_id:abc123". Returns an empty string if no request ID is found.
  11. func formatMetaTag(ctx context.Context) string {
  12. if requestID := reqid.Get(ctx); requestID != "" {
  13. return fmt.Sprintf("%s:%s", requestIDField, requestID)
  14. }
  15. return ""
  16. }
  17. // InfoCtx is a context-aware alternative to Verbose.Info.
  18. // Logs to the INFO log, guarded by the value of v, and prepends a request ID from the context if present.
  19. // Arguments are handled in the manner of fmt.Print.
  20. func (v Verbose) InfoCtx(ctx context.Context, args ...interface{}) {
  21. if !v {
  22. return
  23. }
  24. if metaTag := formatMetaTag(ctx); metaTag != "" {
  25. args = append([]interface{}{metaTag}, args...)
  26. }
  27. logging.print(infoLog, args...)
  28. }
  29. // InfolnCtx is a context-aware alternative to Verbose.Infoln.
  30. // Logs to the INFO log, prepending a request ID from the context if it exists.
  31. // Arguments are handled in the manner of fmt.Println.
  32. func (v Verbose) InfolnCtx(ctx context.Context, args ...interface{}) {
  33. if !v {
  34. return
  35. }
  36. if metaTag := formatMetaTag(ctx); metaTag != "" {
  37. args = append([]interface{}{metaTag}, args...)
  38. }
  39. logging.println(infoLog, args...)
  40. }
  41. // InfofCtx is a context-aware alternative to Verbose.Infof.
  42. // Logs to the INFO log, guarded by the value of v, and prepends a request ID from the context if present.
  43. // Arguments are handled in the manner of fmt.Printf.
  44. func (v Verbose) InfofCtx(ctx context.Context, format string, args ...interface{}) {
  45. if !v {
  46. return
  47. }
  48. if metaTag := formatMetaTag(ctx); metaTag != "" {
  49. format = metaTag + " " + format
  50. }
  51. logging.printf(infoLog, format, args...)
  52. }
  53. // InfofCtx logs a formatted message at info level, prepending a request ID from
  54. // the context if it exists. This is a context-aware alternative to Infof.
  55. func InfofCtx(ctx context.Context, format string, args ...interface{}) {
  56. if metaTag := formatMetaTag(ctx); metaTag != "" {
  57. format = metaTag + " " + format
  58. }
  59. logging.printf(infoLog, format, args...)
  60. }
  61. // InfoCtx logs a message at info level, prepending a request ID from the context
  62. // if it exists. This is a context-aware alternative to Info.
  63. func InfoCtx(ctx context.Context, args ...interface{}) {
  64. if metaTag := formatMetaTag(ctx); metaTag != "" {
  65. args = append([]interface{}{metaTag}, args...)
  66. }
  67. logging.print(infoLog, args...)
  68. }
  69. // WarningCtx logs to the WARNING and INFO logs.
  70. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  71. // This is a context-aware alternative to Warning.
  72. func WarningCtx(ctx context.Context, args ...interface{}) {
  73. if metaTag := formatMetaTag(ctx); metaTag != "" {
  74. args = append([]interface{}{metaTag}, args...)
  75. }
  76. logging.print(warningLog, args...)
  77. }
  78. // WarningDepthCtx logs to the WARNING and INFO logs with a custom call depth.
  79. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  80. // This is a context-aware alternative to WarningDepth.
  81. func WarningDepthCtx(ctx context.Context, depth int, args ...interface{}) {
  82. if metaTag := formatMetaTag(ctx); metaTag != "" {
  83. args = append([]interface{}{metaTag}, args...)
  84. }
  85. logging.printDepth(warningLog, depth, args...)
  86. }
  87. // WarninglnCtx logs to the WARNING and INFO logs.
  88. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
  89. // This is a context-aware alternative to Warningln.
  90. func WarninglnCtx(ctx context.Context, args ...interface{}) {
  91. if metaTag := formatMetaTag(ctx); metaTag != "" {
  92. args = append([]interface{}{metaTag}, args...)
  93. }
  94. logging.println(warningLog, args...)
  95. }
  96. // WarningfCtx logs to the WARNING and INFO logs.
  97. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
  98. // This is a context-aware alternative to Warningf.
  99. func WarningfCtx(ctx context.Context, format string, args ...interface{}) {
  100. if metaTag := formatMetaTag(ctx); metaTag != "" {
  101. format = metaTag + " " + format
  102. }
  103. logging.printf(warningLog, format, args...)
  104. }
  105. // ErrorCtx logs to the ERROR, WARNING, and INFO logs.
  106. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  107. // This is a context-aware alternative to Error.
  108. func ErrorCtx(ctx context.Context, args ...interface{}) {
  109. if metaTag := formatMetaTag(ctx); metaTag != "" {
  110. args = append([]interface{}{metaTag}, args...)
  111. }
  112. logging.print(errorLog, args...)
  113. }
  114. // ErrorDepthCtx logs to the ERROR, WARNING, and INFO logs with a custom call depth.
  115. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  116. // This is a context-aware alternative to ErrorDepth.
  117. func ErrorDepthCtx(ctx context.Context, depth int, args ...interface{}) {
  118. if metaTag := formatMetaTag(ctx); metaTag != "" {
  119. args = append([]interface{}{metaTag}, args...)
  120. }
  121. logging.printDepth(errorLog, depth, args...)
  122. }
  123. // ErrorlnCtx logs to the ERROR, WARNING, and INFO logs.
  124. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
  125. // This is a context-aware alternative to Errorln.
  126. func ErrorlnCtx(ctx context.Context, args ...interface{}) {
  127. if metaTag := formatMetaTag(ctx); metaTag != "" {
  128. args = append([]interface{}{metaTag}, args...)
  129. }
  130. logging.println(errorLog, args...)
  131. }
  132. // ErrorfCtx logs to the ERROR, WARNING, and INFO logs.
  133. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
  134. // This is a context-aware alternative to Errorf.
  135. func ErrorfCtx(ctx context.Context, format string, args ...interface{}) {
  136. if metaTag := formatMetaTag(ctx); metaTag != "" {
  137. format = metaTag + " " + format
  138. }
  139. logging.printf(errorLog, format, args...)
  140. }
  141. // FatalCtx logs to the FATAL, ERROR, WARNING, and INFO logs,
  142. // including a stack trace of all running goroutines, then calls os.Exit(255).
  143. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  144. // This is a context-aware alternative to Fatal.
  145. func FatalCtx(ctx context.Context, args ...interface{}) {
  146. if metaTag := formatMetaTag(ctx); metaTag != "" {
  147. args = append([]interface{}{metaTag}, args...)
  148. }
  149. logging.print(fatalLog, args...)
  150. }
  151. // FatalDepthCtx logs to the FATAL, ERROR, WARNING, and INFO logs with a custom call depth,
  152. // including a stack trace of all running goroutines, then calls os.Exit(255).
  153. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  154. // This is a context-aware alternative to FatalDepth.
  155. func FatalDepthCtx(ctx context.Context, depth int, args ...interface{}) {
  156. if metaTag := formatMetaTag(ctx); metaTag != "" {
  157. args = append([]interface{}{metaTag}, args...)
  158. }
  159. logging.printDepth(fatalLog, depth, args...)
  160. }
  161. // FatallnCtx logs to the FATAL, ERROR, WARNING, and INFO logs,
  162. // including a stack trace of all running goroutines, then calls os.Exit(255).
  163. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
  164. // This is a context-aware alternative to Fatalln.
  165. func FatallnCtx(ctx context.Context, args ...interface{}) {
  166. if metaTag := formatMetaTag(ctx); metaTag != "" {
  167. args = append([]interface{}{metaTag}, args...)
  168. }
  169. logging.println(fatalLog, args...)
  170. }
  171. // FatalfCtx logs to the FATAL, ERROR, WARNING, and INFO logs,
  172. // including a stack trace of all running goroutines, then calls os.Exit(255).
  173. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
  174. // This is a context-aware alternative to Fatalf.
  175. func FatalfCtx(ctx context.Context, format string, args ...interface{}) {
  176. if metaTag := formatMetaTag(ctx); metaTag != "" {
  177. format = metaTag + " " + format
  178. }
  179. logging.printf(fatalLog, format, args...)
  180. }
  181. // ExitCtx logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
  182. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
  183. // This is a context-aware alternative to ExitCtx
  184. func ExitCtx(ctx context.Context, args ...interface{}) {
  185. atomic.StoreUint32(&fatalNoStacks, 1)
  186. if metaTag := formatMetaTag(ctx); metaTag != "" {
  187. args = append([]interface{}{metaTag}, args...)
  188. }
  189. logging.print(fatalLog, args...)
  190. }
  191. // ExitDepthCtx logs to the FATAL, ERROR, WARNING, and INFO logs with a custom call depth,
  192. // then calls os.Exit(1). Prepends a request ID from the context if it exists.
  193. // Arguments are handled in the manner of fmt.Print.
  194. // This is a context-aware alternative to ExitDepth.
  195. func ExitDepthCtx(ctx context.Context, depth int, args ...interface{}) {
  196. atomic.StoreUint32(&fatalNoStacks, 1)
  197. if metaTag := formatMetaTag(ctx); metaTag != "" {
  198. args = append([]interface{}{metaTag}, args...)
  199. }
  200. logging.printDepth(fatalLog, depth, args...)
  201. }
  202. // ExitlnCtx logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
  203. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
  204. // This is a context-aware alternative to Exitln.
  205. func ExitlnCtx(ctx context.Context, args ...interface{}) {
  206. atomic.StoreUint32(&fatalNoStacks, 1)
  207. if metaTag := formatMetaTag(ctx); metaTag != "" {
  208. args = append([]interface{}{metaTag}, args...)
  209. }
  210. logging.println(fatalLog, args...)
  211. }
  212. // ExitfCtx logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
  213. // Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
  214. // This is a context-aware alternative to Exitf.
  215. func ExitfCtx(ctx context.Context, format string, args ...interface{}) {
  216. atomic.StoreUint32(&fatalNoStacks, 1)
  217. if metaTag := formatMetaTag(ctx); metaTag != "" {
  218. format = metaTag + " " + format
  219. }
  220. logging.printf(fatalLog, format, args...)
  221. }