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)
}
}