intlog.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/debug/gdebug"
  16. "github.com/gogf/gf/internal/utils"
  17. )
  18. const (
  19. stackFilterKey = "/internal/intlog"
  20. )
  21. var (
  22. // isGFDebug marks whether printing GoFrame debug information.
  23. isGFDebug = false
  24. )
  25. func init() {
  26. isGFDebug = utils.IsDebugEnabled()
  27. }
  28. // SetEnabled enables/disables the internal logging manually.
  29. // Note that this function is not concurrent safe, be aware of the DATA RACE.
  30. func SetEnabled(enabled bool) {
  31. // If they're the same, it does not write the `isGFDebug` but only reading operation.
  32. if isGFDebug != enabled {
  33. isGFDebug = enabled
  34. }
  35. }
  36. // Print prints `v` with newline using fmt.Println.
  37. // The parameter `v` can be multiple variables.
  38. func Print(ctx context.Context, v ...interface{}) {
  39. if !isGFDebug {
  40. return
  41. }
  42. doPrint(ctx, fmt.Sprint(v...), false)
  43. }
  44. // Printf prints `v` with format `format` using fmt.Printf.
  45. // The parameter `v` can be multiple variables.
  46. func Printf(ctx context.Context, format string, v ...interface{}) {
  47. if !isGFDebug {
  48. return
  49. }
  50. doPrint(ctx, fmt.Sprintf(format, v...), false)
  51. }
  52. // Error prints `v` with newline using fmt.Println.
  53. // The parameter `v` can be multiple variables.
  54. func Error(ctx context.Context, v ...interface{}) {
  55. if !isGFDebug {
  56. return
  57. }
  58. doPrint(ctx, fmt.Sprint(v...), true)
  59. }
  60. // Errorf prints `v` with format `format` using fmt.Printf.
  61. func Errorf(ctx context.Context, format string, v ...interface{}) {
  62. if !isGFDebug {
  63. return
  64. }
  65. doPrint(ctx, fmt.Sprintf(format, v...), true)
  66. }
  67. // PrintFunc prints the output from function `f`.
  68. // It only calls function `f` if debug mode is enabled.
  69. func PrintFunc(ctx context.Context, f func() string) {
  70. if !isGFDebug {
  71. return
  72. }
  73. s := f()
  74. if s == "" {
  75. return
  76. }
  77. doPrint(ctx, s, false)
  78. }
  79. // ErrorFunc prints the output from function `f`.
  80. // It only calls function `f` if debug mode is enabled.
  81. func ErrorFunc(ctx context.Context, f func() string) {
  82. if !isGFDebug {
  83. return
  84. }
  85. s := f()
  86. if s == "" {
  87. return
  88. }
  89. doPrint(ctx, s, true)
  90. }
  91. func doPrint(ctx context.Context, content string, stack bool) {
  92. if !isGFDebug {
  93. return
  94. }
  95. buffer := bytes.NewBuffer(nil)
  96. buffer.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
  97. buffer.WriteString(" [INTE] ")
  98. buffer.WriteString(file())
  99. buffer.WriteString(" ")
  100. if s := traceIdStr(ctx); s != "" {
  101. buffer.WriteString(s + " ")
  102. }
  103. buffer.WriteString(content)
  104. buffer.WriteString("\n")
  105. if stack {
  106. buffer.WriteString(gdebug.StackWithFilter([]string{stackFilterKey}))
  107. }
  108. fmt.Print(buffer.String())
  109. }
  110. // traceIdStr retrieves and returns the trace id string for logging output.
  111. func traceIdStr(ctx context.Context) string {
  112. if ctx == nil {
  113. return ""
  114. }
  115. spanCtx := trace.SpanContextFromContext(ctx)
  116. if traceId := spanCtx.TraceID(); traceId.IsValid() {
  117. return "{" + traceId.String() + "}"
  118. }
  119. return ""
  120. }
  121. // file returns caller file name along with its line number.
  122. func file() string {
  123. _, p, l := gdebug.CallerWithFilter([]string{stackFilterKey})
  124. return fmt.Sprintf(`%s:%d`, filepath.Base(p), l)
  125. }