intlog.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "github.com/gogf/gf/debug/gdebug"
  13. "github.com/gogf/gf/internal/utils"
  14. "go.opentelemetry.io/otel/trace"
  15. "path/filepath"
  16. "time"
  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. doPrint(ctx, fmt.Sprint(v...), false)
  40. }
  41. // Printf prints `v` with format `format` using fmt.Printf.
  42. // The parameter `v` can be multiple variables.
  43. func Printf(ctx context.Context, format string, v ...interface{}) {
  44. doPrint(ctx, fmt.Sprintf(format, v...), false)
  45. }
  46. // Error prints `v` with newline using fmt.Println.
  47. // The parameter `v` can be multiple variables.
  48. func Error(ctx context.Context, v ...interface{}) {
  49. doPrint(ctx, fmt.Sprint(v...), true)
  50. }
  51. // Errorf prints `v` with format `format` using fmt.Printf.
  52. func Errorf(ctx context.Context, format string, v ...interface{}) {
  53. doPrint(ctx, fmt.Sprintf(format, v...), true)
  54. }
  55. // PrintFunc prints the output from function `f`.
  56. // It only calls function `f` if debug mode is enabled.
  57. func PrintFunc(ctx context.Context, f func() string) {
  58. if !isGFDebug {
  59. return
  60. }
  61. s := f()
  62. if s == "" {
  63. return
  64. }
  65. doPrint(ctx, s, false)
  66. }
  67. // ErrorFunc prints the output from function `f`.
  68. // It only calls function `f` if debug mode is enabled.
  69. func ErrorFunc(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, true)
  78. }
  79. func doPrint(ctx context.Context, content string, stack bool) {
  80. if !isGFDebug {
  81. return
  82. }
  83. buffer := bytes.NewBuffer(nil)
  84. buffer.WriteString(now())
  85. buffer.WriteString(" [INTE] ")
  86. buffer.WriteString(file())
  87. buffer.WriteString(" ")
  88. if s := traceIdStr(ctx); s != "" {
  89. buffer.WriteString(s + " ")
  90. }
  91. buffer.WriteString(content)
  92. buffer.WriteString("\n")
  93. if stack {
  94. buffer.WriteString(gdebug.StackWithFilter(stackFilterKey))
  95. }
  96. fmt.Print(buffer.String())
  97. }
  98. // traceIdStr retrieves and returns the trace id string for logging output.
  99. func traceIdStr(ctx context.Context) string {
  100. if ctx == nil {
  101. return ""
  102. }
  103. spanCtx := trace.SpanContextFromContext(ctx)
  104. if traceId := spanCtx.TraceID(); traceId.IsValid() {
  105. return "{" + traceId.String() + "}"
  106. }
  107. return ""
  108. }
  109. // now returns current time string.
  110. func now() string {
  111. return time.Now().Format("2006-01-02 15:04:05.000")
  112. }
  113. // file returns caller file name along with its line number.
  114. func file() string {
  115. _, p, l := gdebug.CallerWithFilter(stackFilterKey)
  116. return fmt.Sprintf(`%s:%d`, filepath.Base(p), l)
  117. }