Update logging
Change-Id: I13279582aa717edad5d56323866b941db1919404
diff --git a/server/agent/agent.go b/server/agent/agent.go
index 6de4784..ed4706d 100644
--- a/server/agent/agent.go
+++ b/server/agent/agent.go
@@ -3,7 +3,7 @@
import (
"context"
"fmt"
- "log"
+ "log/slog"
"os"
"path/filepath"
"strings"
@@ -11,6 +11,7 @@
"github.com/iomodo/staff/git"
"github.com/iomodo/staff/llm"
+ _ "github.com/iomodo/staff/llm/openai" // Import for side effects (registers provider)
"github.com/iomodo/staff/tm"
)
@@ -58,10 +59,11 @@
gitInterface git.GitInterface
ctx context.Context
cancel context.CancelFunc
+ logger *slog.Logger
}
// NewAgent creates a new agent instance
-func NewAgent(config AgentConfig) (*Agent, error) {
+func NewAgent(config AgentConfig, logger *slog.Logger) (*Agent, error) {
// Validate configuration
if err := validateConfig(config); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
@@ -89,7 +91,7 @@
Timeout: 30 * time.Second,
PullRequestProvider: gerritPRProvider,
}
- gitInterface = git.NewGitWithPullRequests(config.GitRepoPath, gitConfig, gerritPRProvider)
+ gitInterface = git.NewGitWithPullRequests(config.GitRepoPath, gitConfig, gerritPRProvider, logger)
} else {
// Use default git interface (GitHub)
gitInterface = git.DefaultGit(config.GitRepoPath)
@@ -104,6 +106,7 @@
gitInterface: gitInterface,
ctx: ctx,
cancel: cancel,
+ logger: logger,
}
return agent, nil
@@ -134,8 +137,8 @@
// Run starts the agent's main loop
func (a *Agent) Run() error {
- log.Printf("Starting agent %s (%s)", a.Config.Name, a.Config.Role)
- defer log.Printf("Agent %s stopped", a.Config.Name)
+ a.logger.Info("Starting agent", slog.String("name", a.Config.Name), slog.String("role", a.Config.Role))
+ defer a.logger.Info("Agent stopped", slog.String("name", a.Config.Name))
// Initialize git repository if needed
if err := a.initializeGit(); err != nil {
@@ -149,7 +152,7 @@
return a.ctx.Err()
default:
if err := a.processNextTask(); err != nil {
- log.Printf("Error processing task: %v", err)
+ a.logger.Error("Error processing task", slog.String("error", err.Error()))
// Continue running even if there's an error
time.Sleep(30 * time.Second)
}
@@ -159,7 +162,7 @@
// Stop stops the agent
func (a *Agent) Stop() {
- log.Printf("Stopping agent %s", a.Config.Name)
+ a.logger.Info("Stopping agent", slog.String("name", a.Config.Name))
a.cancel()
if a.llmProvider != nil {
a.llmProvider.Close()
@@ -248,7 +251,7 @@
}
}
} else {
- log.Printf("Already on target branch: %s", a.Config.GitBranch)
+ a.logger.Info("Already on target branch", slog.String("branch", a.Config.GitBranch))
}
}
@@ -280,7 +283,7 @@
return nil
}
- log.Printf("Processing task: %s - %s", taskToProcess.ID, taskToProcess.Title)
+ a.logger.Info("Processing task", slog.String("id", taskToProcess.ID), slog.String("title", taskToProcess.Title))
// Start the task
startedTask, err := a.Config.TaskManager.StartTask(ctx, taskToProcess.ID)
@@ -292,7 +295,7 @@
solution, err := a.processTaskWithLLM(startedTask)
if err != nil {
// Mark task as failed or retry
- log.Printf("Failed to process task with LLM: %v", err)
+ a.logger.Error("Failed to process task with LLM", slog.String("error", err.Error()))
return err
}
@@ -306,7 +309,7 @@
return fmt.Errorf("failed to complete task: %w", err)
}
- log.Printf("Successfully completed task: %s", startedTask.ID)
+ a.logger.Info("Successfully completed task", slog.String("id", startedTask.ID))
return nil
}
@@ -416,7 +419,7 @@
if err := a.gitInterface.Push(ctx, "origin", gerritRef, git.PushOptions{}); err != nil {
return fmt.Errorf("failed to push to Gerrit: %w", err)
}
- log.Printf("Created Gerrit change for task %s by pushing to %s", task.ID, gerritRef)
+ a.logger.Info("Created Gerrit change for task", slog.String("id", task.ID), slog.String("ref", gerritRef))
} else {
// For GitHub: Push branch and create PR
if err := a.gitInterface.Push(ctx, "origin", branchName, git.PushOptions{SetUpstream: true}); err != nil {
@@ -438,7 +441,7 @@
return fmt.Errorf("failed to create pull request: %w", err)
}
- log.Printf("Created pull request for task %s: %s (ID: %s)", task.ID, pr.Title, pr.ID)
+ a.logger.Info("Created pull request for task", slog.String("id", task.ID), slog.String("title", pr.Title), slog.String("pr_id", pr.ID))
}
return nil