From d896927841ea74597e2bab5b41463b41676c2201 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 23 Jul 2025 16:04:45 -0300 Subject: [PATCH] fix: structure logging --- internal/cmd/root.go | 6 +++--- internal/llm/agent/agent.go | 6 +++--- internal/llm/provider/anthropic.go | 4 ++-- internal/llm/provider/gemini.go | 4 ++-- internal/llm/provider/openai.go | 4 ++-- internal/llm/tools/glob.go | 2 +- internal/lsp/watcher/watcher.go | 2 +- main.go | 3 +-- 8 files changed, 15 insertions(+), 16 deletions(-) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index d63160992141da26b6a26610b06f1b601213e00d..a34a7a7e369090b3da5c63cb250956ec94c7297a 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -85,14 +85,14 @@ to assist developers in writing, debugging, and understanding code directly from app, err := app.New(ctx, conn, cfg) if err != nil { - slog.Error(fmt.Sprintf("Failed to create app instance: %v", err)) + slog.Error("Failed to create app instance", "error", err) return err } defer app.Shutdown() prompt, err = maybePrependStdin(prompt) if err != nil { - slog.Error(fmt.Sprintf("Failed to read from stdin: %v", err)) + slog.Error("Failed to read from stdin", "error", err) return err } @@ -114,7 +114,7 @@ to assist developers in writing, debugging, and understanding code directly from go app.Subscribe(program) if _, err := program.Run(); err != nil { - slog.Error(fmt.Sprintf("TUI run error: %v", err)) + slog.Error("TUI run error", "error", err) return fmt.Errorf("TUI error: %v", err) } return nil diff --git a/internal/llm/agent/agent.go b/internal/llm/agent/agent.go index 39c762991019f339348efab8cd9b769077e316f5..eb5cd6edd5663d385d2430a35564143b050a562e 100644 --- a/internal/llm/agent/agent.go +++ b/internal/llm/agent/agent.go @@ -227,7 +227,7 @@ func (a *agent) Cancel(sessionID string) { // Cancel regular requests if cancelFunc, exists := a.activeRequests.LoadAndDelete(sessionID); exists { if cancel, ok := cancelFunc.(context.CancelFunc); ok { - slog.Info(fmt.Sprintf("Request cancellation initiated for session: %s", sessionID)) + slog.Info("Request cancellation initiated", "session_id", sessionID) cancel() } } @@ -235,7 +235,7 @@ func (a *agent) Cancel(sessionID string) { // Also check for summarize requests if cancelFunc, exists := a.activeRequests.LoadAndDelete(sessionID + "-summarize"); exists { if cancel, ok := cancelFunc.(context.CancelFunc); ok { - slog.Info(fmt.Sprintf("Summarize cancellation initiated for session: %s", sessionID)) + slog.Info("Summarize cancellation initiated", "session_id", sessionID) cancel() } } @@ -365,7 +365,7 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string }) titleErr := a.generateTitle(context.Background(), sessionID, content) if titleErr != nil && !errors.Is(titleErr, context.Canceled) && !errors.Is(titleErr, context.DeadlineExceeded) { - slog.Error(fmt.Sprintf("failed to generate title: %v", titleErr)) + slog.Error("failed to generate title", "error", titleErr) } }() } diff --git a/internal/llm/provider/anthropic.go b/internal/llm/provider/anthropic.go index 1e8364b08cb76ec7210d9937302cd1c647857b2d..9959cf0f2407f34e93f705bd2f950b1d09fe0f72 100644 --- a/internal/llm/provider/anthropic.go +++ b/internal/llm/provider/anthropic.go @@ -248,7 +248,7 @@ func (a *anthropicClient) send(ctx context.Context, messages []message.Message, return nil, retryErr } if retry { - slog.Warn(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries)) + slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries) select { case <-ctx.Done(): return nil, ctx.Err() @@ -401,7 +401,7 @@ func (a *anthropicClient) stream(ctx context.Context, messages []message.Message return } if retry { - slog.Warn(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries)) + slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries) select { case <-ctx.Done(): // context cancelled diff --git a/internal/llm/provider/gemini.go b/internal/llm/provider/gemini.go index d2aee5090029e207ef1bdf5e0dad8e011e763267..6820a1abaa0795dc5658c249672a270dc2d3b37e 100644 --- a/internal/llm/provider/gemini.go +++ b/internal/llm/provider/gemini.go @@ -210,7 +210,7 @@ func (g *geminiClient) send(ctx context.Context, messages []message.Message, too return nil, retryErr } if retry { - slog.Warn(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries)) + slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries) select { case <-ctx.Done(): return nil, ctx.Err() @@ -323,7 +323,7 @@ func (g *geminiClient) stream(ctx context.Context, messages []message.Message, t return } if retry { - slog.Warn(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries)) + slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries) select { case <-ctx.Done(): if ctx.Err() != nil { diff --git a/internal/llm/provider/openai.go b/internal/llm/provider/openai.go index f55914520774e2fcf5e6283e22365f4ce3621dc1..9a3af8e3cbeae830c0e38c21cd48c9c667443434 100644 --- a/internal/llm/provider/openai.go +++ b/internal/llm/provider/openai.go @@ -222,7 +222,7 @@ func (o *openaiClient) send(ctx context.Context, messages []message.Message, too return nil, retryErr } if retry { - slog.Warn(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries)) + slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries) select { case <-ctx.Done(): return nil, ctx.Err() @@ -395,7 +395,7 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t return } if retry { - slog.Warn(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries)) + slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries) select { case <-ctx.Done(): // context cancelled diff --git a/internal/llm/tools/glob.go b/internal/llm/tools/glob.go index c70c76b7d2dbd798118a54859e5672dacc6e1304..5af6e055574e3f85b68d8616b44d361790c0a3fb 100644 --- a/internal/llm/tools/glob.go +++ b/internal/llm/tools/glob.go @@ -146,7 +146,7 @@ func globFiles(pattern, searchPath string, limit int) ([]string, bool, error) { if err == nil { return matches, len(matches) >= limit && limit > 0, nil } - slog.Warn(fmt.Sprintf("Ripgrep execution failed: %v. Falling back to doublestar.", err)) + slog.Warn("Ripgrep execution failed, falling back to doublestar", "error", err) } return fsext.GlobWithDoubleStar(pattern, searchPath, limit) diff --git a/internal/lsp/watcher/watcher.go b/internal/lsp/watcher/watcher.go index 976bbb291b08b84af578b7e05e2d568cd2ad5d04..080870937bee98be852979748dab456fa6a53b66 100644 --- a/internal/lsp/watcher/watcher.go +++ b/internal/lsp/watcher/watcher.go @@ -90,7 +90,7 @@ func (w *WorkspaceWatcher) AddRegistrations(ctx context.Context, id string, watc slog.Debug("BaseURI", "baseURI", u) } default: - slog.Debug("GlobPattern", "unknown type", fmt.Sprintf("%T", v)) + slog.Debug("GlobPattern unknown type", "type", fmt.Sprintf("%T", v)) } // Log WatchKind diff --git a/main.go b/main.go index 13af1ae45d1b0ef70c72dbdc2a684f9e7c46d3a3..072e3b35d2a2f408d8ed6a09423712b324df8b96 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log/slog" "net/http" "os" @@ -23,7 +22,7 @@ func main() { go func() { slog.Info("Serving pprof at localhost:6060") if httpErr := http.ListenAndServe("localhost:6060", nil); httpErr != nil { - slog.Error(fmt.Sprintf("Failed to pprof listen: %v", httpErr)) + slog.Error("Failed to pprof listen", "error", httpErr) } }() }