feat: add logrus logger

This commit is contained in:
2026-03-30 16:59:45 -05:00
parent 5e23e20e5a
commit b3eb75646a
7 changed files with 38 additions and 22 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"math/rand"
"time"
@@ -11,6 +10,7 @@ import (
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/models"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/sirupsen/logrus"
)
func generateTasks(count int) []models.Task {
@@ -46,32 +46,37 @@ func saveTasks(db *pgxpool.Pool, ctx context.Context, tasks []models.Task) error
}
duration := time.Since(start)
fmt.Printf("Se insertaron exitosamente %d registros en %v\n", rowsCopied, duration)
logrus.Infof("Se insertaron exitosamente %d registros en %v\n", rowsCopied, duration)
return nil
}
func main() {
fmt.Println("Starting seed process")
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.StampMilli,
})
logrus.Info("Starting seed process")
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
db, err := pgxpool.New(ctx, config.Db.Url)
if err != nil {
log.Fatalf("Unable to create connection pool: %v", err)
logrus.Fatalf("Unable to create connection pool: %v", err)
} else {
fmt.Println("Successfully connected to the database")
logrus.Info("Successfully connected to the database")
}
defer db.Close()
count := 100000
fmt.Printf("Generando %d registros...\n", count)
logrus.Infof("Generando %d registros...\n", count)
tasks := generateTasks(count)
if err := saveTasks(db, ctx, tasks); err != nil {
log.Fatalf("Unexpected error: %v", err)
logrus.Fatalf("Unexpected error: %v", err)
} else {
fmt.Println("Database seed completed succesfully")
logrus.Info("Database seed completed succesfully")
}
}