perf: avoid eager formatting in debug log calls (#4929)

This commit is contained in:
Seefs
2026-05-19 12:11:24 +08:00
committed by GitHub
parent 5dd0d3bcbd
commit 0936e25046
33 changed files with 110 additions and 149 deletions
+9 -4
View File
@@ -95,9 +95,11 @@ func LogDebug(ctx context.Context, msg string, args ...any) {
}
func logHelper(ctx context.Context, level string, msg string) {
id := ctx.Value(common.RequestIdKey)
if id == nil {
id = "SYSTEM"
var id any = "SYSTEM"
if ctx != nil {
if requestID := ctx.Value(common.RequestIdKey); requestID != nil {
id = requestID
}
}
now := time.Now()
common.LogWriterMu.RLock()
@@ -172,10 +174,13 @@ func FormatQuota(quota int) string {
// LogJson 仅供测试使用 only for test
func LogJson(ctx context.Context, msg string, obj any) {
if !common.DebugEnabled {
return
}
jsonStr, err := common.Marshal(obj)
if err != nil {
LogError(ctx, fmt.Sprintf("json marshal failed: %s", err.Error()))
return
}
LogDebug(ctx, fmt.Sprintf("%s | %s", msg, string(jsonStr)))
LogDebug(ctx, "%s | %s", msg, jsonStr)
}