Refactor prism and go sdk logging and clean up messages#36484
Refactor prism and go sdk logging and clean up messages#36484shunping merged 4 commits intoapache:masterfrom
Conversation
|
Assigning reviewers: R: @jrmccluskey for label go. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request effectively refactors the logging system for Prism and the Go SDK to use Go's structured logging library, slog. The changes centralize logging configuration, reduce log verbosity by default, and improve the structure and clarity of log messages, aligning with the goal of a more user-friendly logging experience. The refactoring is well-executed. I have a couple of suggestions to further improve the clarity and consistency of the new logging output.
| case "dev": | ||
| logHandler = | ||
| devslog.NewHandler(loggerOutput, &devslog.Options{ | ||
| TimeFormat: "[" + time.RFC3339Nano + "]", | ||
| StringerFormatter: true, | ||
| HandlerOptions: handlerOpts, | ||
| StringIndentation: false, | ||
| NewLineAfterLog: true, | ||
| MaxErrorStackTrace: 3, | ||
| }) |
There was a problem hiding this comment.
The configuration for the dev log kind uses devslog, which produces a human-readable format (e.g., [<timestamp>] INFO ...). However, the 'After' log example in the pull request description shows a key=value format, which corresponds to the text log kind (slog.TextHandler). Since the default log_kind for Prism is dev, this discrepancy could be confusing. To align the default behavior with the example, you might consider changing the default log_kind flag in sdks/go/cmd/prism/prism.go to text, or updating the PR description to show an example of the devslog output.
| var b strings.Builder | ||
| if resp.GetTime() != "" { | ||
| fmt.Fprintf(&b, "(time=%v)", resp.GetTime()) | ||
| } | ||
| if resp.GetMessageId() != "" { | ||
| fmt.Fprintf(&b, "(id=%v)", resp.GetMessageId()) | ||
| } | ||
| b.WriteString(resp.GetMessageText()) | ||
| text := b.String() | ||
|
|
There was a problem hiding this comment.
Using strings.Builder is efficient, but the resulting log format (time=...)(id=...)message can be a bit hard to read, especially with no space between the parts. For improved readability and a more standard log appearance, consider building the message from parts with spaces in between. A slice-based approach can be more readable here, and the performance difference is likely negligible in this context.
| var b strings.Builder | |
| if resp.GetTime() != "" { | |
| fmt.Fprintf(&b, "(time=%v)", resp.GetTime()) | |
| } | |
| if resp.GetMessageId() != "" { | |
| fmt.Fprintf(&b, "(id=%v)", resp.GetMessageId()) | |
| } | |
| b.WriteString(resp.GetMessageText()) | |
| text := b.String() | |
| var parts []string | |
| if resp.GetTime() != "" { | |
| parts = append(parts, fmt.Sprintf("time=%v", resp.GetTime())) | |
| } | |
| if resp.GetMessageId() != "" { | |
| parts = append(parts, fmt.Sprintf("id=%v", resp.GetMessageId())) | |
| } | |
| if resp.GetMessageText() != "" { | |
| parts = append(parts, resp.GetMessageText()) | |
| } | |
| text := strings.Join(parts, " ") |
A cleaner log by default is more user-friendly. It also helps to check test output without losing in the swarm of messages.
Before:
After: