intlog.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package intlog provides internal logging for GoFrame development usage only.
  7. package intlog
  8. import (
  9. "bytes"
  10. "context"
  11. "fmt"
  12. "path/filepath"
  13. "time"
  14. "go.opentelemetry.io/otel/trace"
  15. "github.com/gogf/gf/v2/debug/gdebug"
  16. "github.com/gogf/gf/v2/internal/utils"
  17. )
  18. const (
  19. stackFilterKey = "/internal/intlog"
  20. )
  21. // Print prints `v` with newline using fmt.Println.
  22. // The parameter `v` can be multiple variables.
  23. func Print(ctx context.Context, v ...interface{}) {
  24. if !utils.IsDebugEnabled() {
  25. return
  26. }
  27. doPrint(ctx, fmt.Sprint(v...), false)
  28. }
  29. // Printf prints `v` with format `format` using fmt.Printf.
  30. // The parameter `v` can be multiple variables.
  31. func Printf(ctx context.Context, format string, v ...interface{}) {
  32. if !utils.IsDebugEnabled() {
  33. return
  34. }
  35. doPrint(ctx, fmt.Sprintf(format, v...), false)
  36. }
  37. // Error prints `v` with newline using fmt.Println.
  38. // The parameter `v` can be multiple variables.
  39. func Error(ctx context.Context, v ...interface{}) {
  40. if !utils.IsDebugEnabled() {
  41. return
  42. }
  43. doPrint(ctx, fmt.Sprint(v...), true)
  44. }
  45. // Errorf prints `v` with format `format` using fmt.Printf.
  46. func Errorf(ctx context.Context, format string, v ...interface{}) {
  47. if !utils.IsDebugEnabled() {
  48. return
  49. }
  50. doPrint(ctx, fmt.Sprintf(format, v...), true)
  51. }
  52. // PrintFunc prints the output from function `f`.
  53. // It only calls function `f` if debug mode is enabled.
  54. func PrintFunc(ctx context.Context, f func() string) {
  55. if !utils.IsDebugEnabled() {
  56. return
  57. }
  58. s := f()
  59. if s == "" {
  60. return
  61. }
  62. doPrint(ctx, s, false)
  63. }
  64. // ErrorFunc prints the output from function `f`.
  65. // It only calls function `f` if debug mode is enabled.
  66. func ErrorFunc(ctx context.Context, f func() string) {
  67. if !utils.IsDebugEnabled() {
  68. return
  69. }
  70. s := f()
  71. if s == "" {
  72. return
  73. }
  74. doPrint(ctx, s, true)
  75. }
  76. func doPrint(ctx context.Context, content string, stack bool) {
  77. if !utils.IsDebugEnabled() {
  78. return
  79. }
  80. buffer := bytes.NewBuffer(nil)
  81. buffer.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
  82. buffer.WriteString(" [INTE] ")
  83. buffer.WriteString(file())
  84. buffer.WriteString(" ")
  85. if s := traceIdStr(ctx); s != "" {
  86. buffer.WriteString(s + " ")
  87. }
  88. buffer.WriteString(content)
  89. buffer.WriteString("\n")
  90. if stack {
  91. buffer.WriteString("Caller Stack:\n")
  92. buffer.WriteString(gdebug.StackWithFilter([]string{stackFilterKey}))
  93. }
  94. fmt.Print(buffer.String())
  95. }
  96. // traceIdStr retrieves and returns the trace id string for logging output.
  97. func traceIdStr(ctx context.Context) string {
  98. if ctx == nil {
  99. return ""
  100. }
  101. spanCtx := trace.SpanContextFromContext(ctx)
  102. if traceId := spanCtx.TraceID(); traceId.IsValid() {
  103. return "{" + traceId.String() + "}"
  104. }
  105. return ""
  106. }
  107. // file returns caller file name along with its line number.
  108. func file() string {
  109. _, p, l := gdebug.CallerWithFilter([]string{stackFilterKey})
  110. return fmt.Sprintf(`%s:%d`, filepath.Base(p), l)
  111. }