github.com/gogf/gf/v2@v2.7.4/internal/intlog/intlog.go (about) 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 7 // Package intlog provides internal logging for GoFrame development usage only. 8 package intlog 9 10 import ( 11 "bytes" 12 "context" 13 "fmt" 14 "path/filepath" 15 "time" 16 17 "go.opentelemetry.io/otel/trace" 18 19 "github.com/gogf/gf/v2/debug/gdebug" 20 "github.com/gogf/gf/v2/internal/utils" 21 ) 22 23 const ( 24 stackFilterKey = "/internal/intlog" 25 ) 26 27 // Print prints `v` with newline using fmt.Println. 28 // The parameter `v` can be multiple variables. 29 func Print(ctx context.Context, v ...interface{}) { 30 if !utils.IsDebugEnabled() { 31 return 32 } 33 doPrint(ctx, fmt.Sprint(v...), false) 34 } 35 36 // Printf prints `v` with format `format` using fmt.Printf. 37 // The parameter `v` can be multiple variables. 38 func Printf(ctx context.Context, format string, v ...interface{}) { 39 if !utils.IsDebugEnabled() { 40 return 41 } 42 doPrint(ctx, fmt.Sprintf(format, v...), false) 43 } 44 45 // Error prints `v` with newline using fmt.Println. 46 // The parameter `v` can be multiple variables. 47 func Error(ctx context.Context, v ...interface{}) { 48 if !utils.IsDebugEnabled() { 49 return 50 } 51 doPrint(ctx, fmt.Sprint(v...), true) 52 } 53 54 // Errorf prints `v` with format `format` using fmt.Printf. 55 func Errorf(ctx context.Context, format string, v ...interface{}) { 56 if !utils.IsDebugEnabled() { 57 return 58 } 59 doPrint(ctx, fmt.Sprintf(format, v...), true) 60 } 61 62 // PrintFunc prints the output from function `f`. 63 // It only calls function `f` if debug mode is enabled. 64 func PrintFunc(ctx context.Context, f func() string) { 65 if !utils.IsDebugEnabled() { 66 return 67 } 68 s := f() 69 if s == "" { 70 return 71 } 72 doPrint(ctx, s, false) 73 } 74 75 // ErrorFunc prints the output from function `f`. 76 // It only calls function `f` if debug mode is enabled. 77 func ErrorFunc(ctx context.Context, f func() string) { 78 if !utils.IsDebugEnabled() { 79 return 80 } 81 s := f() 82 if s == "" { 83 return 84 } 85 doPrint(ctx, s, true) 86 } 87 88 func doPrint(ctx context.Context, content string, stack bool) { 89 if !utils.IsDebugEnabled() { 90 return 91 } 92 buffer := bytes.NewBuffer(nil) 93 buffer.WriteString(time.Now().Format("2006-01-02 15:04:05.000")) 94 buffer.WriteString(" [INTE] ") 95 buffer.WriteString(file()) 96 buffer.WriteString(" ") 97 if s := traceIdStr(ctx); s != "" { 98 buffer.WriteString(s + " ") 99 } 100 buffer.WriteString(content) 101 buffer.WriteString("\n") 102 if stack { 103 buffer.WriteString("Caller Stack:\n") 104 buffer.WriteString(gdebug.StackWithFilter([]string{stackFilterKey})) 105 } 106 fmt.Print(buffer.String()) 107 } 108 109 // traceIdStr retrieves and returns the trace id string for logging output. 110 func traceIdStr(ctx context.Context) string { 111 if ctx == nil { 112 return "" 113 } 114 spanCtx := trace.SpanContextFromContext(ctx) 115 if traceId := spanCtx.TraceID(); traceId.IsValid() { 116 return "{" + traceId.String() + "}" 117 } 118 return "" 119 } 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 }