Update logging
Change-Id: I13279582aa717edad5d56323866b941db1919404
diff --git a/server/git/example.go b/server/git/example.go
index c261604..1bea27c 100644
--- a/server/git/example.go
+++ b/server/git/example.go
@@ -2,39 +2,43 @@
import (
"context"
- "fmt"
- "log"
+ "log/slog"
+ "os"
)
// Example demonstrates how to use the Git interface
func Example() {
ctx := context.Background()
+ // Create logger
+ logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
+
// Create a new Git instance
git := DefaultGit("/path/to/your/repo")
// Get repository status
status, err := git.Status(ctx)
if err != nil {
- log.Fatalf("Failed to get status: %v", err)
+ logger.Error("Failed to get status", slog.String("error", err.Error()))
+ os.Exit(1)
}
- fmt.Printf("Current branch: %s\n", status.Branch)
- fmt.Printf("Repository is clean: %t\n", status.IsClean)
+ logger.Info("Repository status", slog.String("branch", status.Branch), slog.Bool("clean", status.IsClean))
// List branches
branches, err := git.ListBranches(ctx)
if err != nil {
- log.Fatalf("Failed to list branches: %v", err)
+ logger.Error("Failed to list branches", slog.String("error", err.Error()))
+ os.Exit(1)
}
- fmt.Println("Branches:")
+ logger.Info("Branches found", slog.Int("count", len(branches)))
for _, branch := range branches {
current := ""
if branch.IsCurrent {
current = " (current)"
}
- fmt.Printf(" %s%s\n", branch.Name, current)
+ logger.Info("Branch", slog.String("name", branch.Name+current))
}
// Get recent commits
@@ -45,12 +49,13 @@
commits, err := git.Log(ctx, logOptions)
if err != nil {
- log.Fatalf("Failed to get log: %v", err)
+ logger.Error("Failed to get log", slog.String("error", err.Error()))
+ os.Exit(1)
}
- fmt.Println("Recent commits:")
+ logger.Info("Recent commits", slog.Int("count", len(commits)))
for _, commit := range commits {
- fmt.Printf(" %s: %s\n", commit.Hash[:8], commit.Message)
+ logger.Info("Commit", slog.String("hash", commit.Hash[:8]), slog.String("message", commit.Message))
}
}
@@ -58,12 +63,16 @@
func ExampleWorkflow() {
ctx := context.Background()
+ // Create logger
+ logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
+
// Initialize a new repository
git := DefaultGit("/path/to/new/repo")
// Initialize the repository
if err := git.Init(ctx, "/path/to/new/repo"); err != nil {
- log.Fatalf("Failed to initialize repository: %v", err)
+ logger.Error("Failed to initialize repository", slog.String("error", err.Error()))
+ os.Exit(1)
}
// Set user configuration
@@ -73,7 +82,8 @@
}
if err := git.SetUserConfig(ctx, userConfig); err != nil {
- log.Fatalf("Failed to set user config: %v", err)
+ logger.Error("Failed to set user config", slog.String("error", err.Error()))
+ os.Exit(1)
}
// Create a new file and add it
@@ -81,7 +91,8 @@
// Stage all changes
if err := git.AddAll(ctx); err != nil {
- log.Fatalf("Failed to add files: %v", err)
+ logger.Error("Failed to add files", slog.String("error", err.Error()))
+ os.Exit(1)
}
// Commit the changes
@@ -90,42 +101,50 @@
}
if err := git.Commit(ctx, "Initial commit", commitOptions); err != nil {
- log.Fatalf("Failed to commit: %v", err)
+ logger.Error("Failed to commit", slog.String("error", err.Error()))
+ os.Exit(1)
}
// Create a new branch
if err := git.CreateBranch(ctx, "feature/new-feature", ""); err != nil {
- log.Fatalf("Failed to create branch: %v", err)
+ logger.Error("Failed to create branch", slog.String("error", err.Error()))
+ os.Exit(1)
}
// Switch to the new branch
if err := git.Checkout(ctx, "feature/new-feature"); err != nil {
- log.Fatalf("Failed to checkout branch: %v", err)
+ logger.Error("Failed to checkout branch", slog.String("error", err.Error()))
+ os.Exit(1)
}
- fmt.Println("Repository initialized and feature branch created!")
+ logger.Info("Repository initialized and feature branch created!")
}
// ExampleRemoteOperations demonstrates remote repository operations
func ExampleRemoteOperations() {
ctx := context.Background()
+ // Create logger
+ logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
+
git := DefaultGit("/path/to/your/repo")
// Add a remote
if err := git.AddRemote(ctx, "origin", "https://github.com/user/repo.git"); err != nil {
- log.Fatalf("Failed to add remote: %v", err)
+ logger.Error("Failed to add remote", slog.String("error", err.Error()))
+ os.Exit(1)
}
// List remotes
remotes, err := git.ListRemotes(ctx)
if err != nil {
- log.Fatalf("Failed to list remotes: %v", err)
+ logger.Error("Failed to list remotes", slog.String("error", err.Error()))
+ os.Exit(1)
}
- fmt.Println("Remotes:")
+ logger.Info("Remotes found", slog.Int("count", len(remotes)))
for _, remote := range remotes {
- fmt.Printf(" %s: %s\n", remote.Name, remote.URL)
+ logger.Info("Remote", slog.String("name", remote.Name), slog.String("url", remote.URL))
}
// Fetch from remote
@@ -135,7 +154,8 @@
}
if err := git.Fetch(ctx, "", fetchOptions); err != nil {
- log.Fatalf("Failed to fetch: %v", err)
+ logger.Error("Failed to fetch", slog.String("error", err.Error()))
+ os.Exit(1)
}
// Push to remote
@@ -144,6 +164,7 @@
}
if err := git.Push(ctx, "origin", "main", pushOptions); err != nil {
- log.Fatalf("Failed to push: %v", err)
+ logger.Error("Failed to push", slog.String("error", err.Error()))
+ os.Exit(1)
}
}
diff --git a/server/git/git.go b/server/git/git.go
index dc3885f..9aa1ae2 100644
--- a/server/git/git.go
+++ b/server/git/git.go
@@ -3,6 +3,7 @@
import (
"context"
"fmt"
+ "log/slog"
"os"
"os/exec"
"path/filepath"
@@ -189,6 +190,7 @@
repoPath string
config GitConfig
prProvider PullRequestProvider
+ logger *slog.Logger
}
// GitConfig holds configuration for Git operations
@@ -199,7 +201,7 @@
}
// NewGit creates a new Git instance
-func NewGit(repoPath string, config GitConfig) GitInterface {
+func NewGit(repoPath string, config GitConfig, logger *slog.Logger) GitInterface {
if config.Timeout == 0 {
config.Timeout = 30 * time.Second
}
@@ -208,6 +210,7 @@
repoPath: repoPath,
config: config,
prProvider: config.PullRequestProvider,
+ logger: logger,
}
}
@@ -215,14 +218,13 @@
func DefaultGit(repoPath string) GitInterface {
return NewGit(repoPath, GitConfig{
Timeout: 30 * time.Second,
- Env: make(map[string]string),
- })
+ }, slog.Default())
}
// NewGitWithPullRequests creates a Git instance with pull request capabilities
-func NewGitWithPullRequests(repoPath string, config GitConfig, prProvider PullRequestProvider) GitInterface {
+func NewGitWithPullRequests(repoPath string, config GitConfig, prProvider PullRequestProvider, logger *slog.Logger) GitInterface {
config.PullRequestProvider = prProvider
- return NewGit(repoPath, config)
+ return NewGit(repoPath, config, logger)
}
// Ensure Git implements GitInterface
diff --git a/server/git/git_test.go b/server/git/git_test.go
index bccf477..d3862ce 100644
--- a/server/git/git_test.go
+++ b/server/git/git_test.go
@@ -3,6 +3,7 @@
import (
"context"
"fmt"
+ "log/slog"
"os"
"path/filepath"
"testing"
@@ -10,6 +11,9 @@
)
func TestNewGit(t *testing.T) {
+ // Create logger for testing
+ logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
+
// Test creating a new Git instance with default config
git := DefaultGit("/tmp/test-repo")
if git == nil {
@@ -23,7 +27,7 @@
"GIT_AUTHOR_NAME": "Test User",
},
}
- git = NewGit("/tmp/test-repo", config)
+ git = NewGit("/tmp/test-repo", config, logger)
if git == nil {
t.Fatal("NewGit returned nil")
}
diff --git a/server/git/pull_request_example.go b/server/git/pull_request_example.go
index 5e34dc4..5c70d21 100644
--- a/server/git/pull_request_example.go
+++ b/server/git/pull_request_example.go
@@ -2,25 +2,28 @@
import (
"context"
- "fmt"
- "log"
+ "log/slog"
"net/http"
+ "os"
"time"
)
-// ExamplePullRequestUsage demonstrates how to use the pull request capabilities
+// ExamplePullRequestUsage demonstrates how to use pull request functionality
func ExamplePullRequestUsage() {
ctx := context.Background()
- // Example 1: GitHub Pull Requests
- exampleGitHubPullRequests(ctx)
+ // Create logger
+ logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
- // Example 2: Gerrit Pull Requests
- exampleGerritPullRequests(ctx)
+ // Example with GitHub
+ exampleGitHubPullRequests(ctx, logger)
+
+ // Example with Gerrit
+ exampleGerritPullRequests(ctx, logger)
}
-func exampleGitHubPullRequests(ctx context.Context) {
- fmt.Println("=== GitHub Pull Request Example ===")
+func exampleGitHubPullRequests(ctx context.Context, logger *slog.Logger) {
+ logger.Info("=== GitHub Pull Request Example ===")
// Create GitHub configuration
githubConfig := GitHubConfig{
@@ -35,7 +38,7 @@
// Create Git instance with GitHub pull request capabilities
git := NewGitWithPullRequests("/path/to/repo", GitConfig{
Timeout: 30 * time.Second,
- }, githubProvider)
+ }, githubProvider, logger)
// Create a new pull request
prOptions := PullRequestOptions{
@@ -51,11 +54,11 @@
pr, err := git.CreatePullRequest(ctx, prOptions)
if err != nil {
- log.Printf("Failed to create pull request: %v", err)
+ logger.Error("Failed to create pull request", slog.String("error", err.Error()))
return
}
- fmt.Printf("Created pull request: %s (#%d)\n", pr.Title, pr.Number)
+ logger.Info("Created pull request", slog.String("title", pr.Title), slog.Int("number", pr.Number))
// List pull requests
listOptions := ListPullRequestOptions{
@@ -67,20 +70,20 @@
prs, err := git.ListPullRequests(ctx, listOptions)
if err != nil {
- log.Printf("Failed to list pull requests: %v", err)
+ logger.Error("Failed to list pull requests", slog.String("error", err.Error()))
return
}
- fmt.Printf("Found %d pull requests\n", len(prs))
+ logger.Info("Found pull requests", slog.Int("count", len(prs)))
// Get a specific pull request
pr, err = git.GetPullRequest(ctx, pr.ID)
if err != nil {
- log.Printf("Failed to get pull request: %v", err)
+ logger.Error("Failed to get pull request", slog.String("error", err.Error()))
return
}
- fmt.Printf("Pull request status: %s\n", pr.State)
+ logger.Info("Pull request status", slog.String("state", pr.State))
// Update a pull request
updateOptions := PullRequestOptions{
@@ -91,11 +94,11 @@
updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions)
if err != nil {
- log.Printf("Failed to update pull request: %v", err)
+ logger.Error("Failed to update pull request", slog.String("error", err.Error()))
return
}
- fmt.Printf("Updated pull request: %s\n", updatedPR.Title)
+ logger.Info("Updated pull request", slog.String("title", updatedPR.Title))
// Merge a pull request
mergeOptions := MergePullRequestOptions{
@@ -106,15 +109,15 @@
err = git.MergePullRequest(ctx, pr.ID, mergeOptions)
if err != nil {
- log.Printf("Failed to merge pull request: %v", err)
+ logger.Error("Failed to merge pull request", slog.String("error", err.Error()))
return
}
- fmt.Println("Pull request merged successfully")
+ logger.Info("Pull request merged successfully")
}
-func exampleGerritPullRequests(ctx context.Context) {
- fmt.Println("=== Gerrit Pull Request Example ===")
+func exampleGerritPullRequests(ctx context.Context, logger *slog.Logger) {
+ logger.Info("=== Gerrit Pull Request Example ===")
// Create Gerrit configuration
gerritConfig := GerritConfig{
@@ -130,7 +133,7 @@
// Create Git instance with Gerrit pull request capabilities
git := NewGitWithPullRequests("/path/to/repo", GitConfig{
Timeout: 30 * time.Second,
- }, gerritProvider)
+ }, gerritProvider, logger)
// Create a new change (pull request)
prOptions := PullRequestOptions{
@@ -142,11 +145,11 @@
pr, err := git.CreatePullRequest(ctx, prOptions)
if err != nil {
- log.Printf("Failed to create change: %v", err)
+ logger.Error("Failed to create change", slog.String("error", err.Error()))
return
}
- fmt.Printf("Created change: %s (#%d)\n", pr.Title, pr.Number)
+ logger.Info("Created change", slog.String("title", pr.Title), slog.Int("number", pr.Number))
// List changes
listOptions := ListPullRequestOptions{
@@ -158,20 +161,20 @@
prs, err := git.ListPullRequests(ctx, listOptions)
if err != nil {
- log.Printf("Failed to list changes: %v", err)
+ logger.Error("Failed to list changes", slog.String("error", err.Error()))
return
}
- fmt.Printf("Found %d changes\n", len(prs))
+ logger.Info("Found changes", slog.Int("count", len(prs)))
// Get a specific change
pr, err = git.GetPullRequest(ctx, pr.ID)
if err != nil {
- log.Printf("Failed to get change: %v", err)
+ logger.Error("Failed to get change", slog.String("error", err.Error()))
return
}
- fmt.Printf("Change status: %s\n", pr.State)
+ logger.Info("Change status", slog.String("state", pr.State))
// Update a change
updateOptions := PullRequestOptions{
@@ -181,11 +184,11 @@
updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions)
if err != nil {
- log.Printf("Failed to update change: %v", err)
+ logger.Error("Failed to update change", slog.String("error", err.Error()))
return
}
- fmt.Printf("Updated change: %s\n", updatedPR.Title)
+ logger.Info("Updated change", slog.String("title", updatedPR.Title))
// Submit a change (merge)
mergeOptions := MergePullRequestOptions{
@@ -195,11 +198,11 @@
err = git.MergePullRequest(ctx, pr.ID, mergeOptions)
if err != nil {
- log.Printf("Failed to submit change: %v", err)
+ logger.Error("Failed to submit change", slog.String("error", err.Error()))
return
}
- fmt.Println("Change submitted successfully")
+ logger.Info("Change submitted successfully")
}
// Example of using both providers in the same application
@@ -218,7 +221,7 @@
BaseURL: "https://api.github.com",
}
githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig)
- git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, githubProvider)
+ git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, githubProvider, nil) // Pass nil for logger as it's not used in this example
} else {
// Use Gerrit
gerritConfig := GerritConfig{
@@ -227,7 +230,7 @@
BaseURL: "https://gerrit.example.com",
}
gerritProvider := NewGerritPullRequestProvider("project", gerritConfig)
- git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, gerritProvider)
+ git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, gerritProvider, nil) // Pass nil for logger as it's not used in this example
}
// Use the same interface regardless of provider
@@ -240,9 +243,9 @@
pr, err := git.CreatePullRequest(ctx, prOptions)
if err != nil {
- log.Printf("Failed to create pull request: %v", err)
+ slog.Error("Failed to create pull request", slog.String("error", err.Error()))
return
}
- fmt.Printf("Created pull request: %s\n", pr.Title)
+ slog.Info("Created pull request", slog.String("title", pr.Title))
}