feat: add logrus logger
This commit is contained in:
@@ -3,11 +3,11 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/config"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func dropTables(db *pgxpool.Pool, ctx context.Context) error {
|
||||
@@ -27,13 +27,13 @@ func main() {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if err := dropTables(db, ctx); err != nil {
|
||||
log.Fatalf("Unexpected error: %v", err)
|
||||
logrus.Fatalf("Unexpected error: %v", err)
|
||||
} else {
|
||||
fmt.Println("Database reset completed succesfully")
|
||||
logrus.Info("Database reset completed succesfully")
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/config"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func createTasksTable(db *pgxpool.Pool, ctx context.Context) error {
|
||||
@@ -30,18 +30,23 @@ CREATE TABLE IF NOT EXISTS tasks (
|
||||
}
|
||||
|
||||
func main() {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: time.StampMilli,
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if err := createTasksTable(db, ctx); err != nil {
|
||||
log.Fatalf("Unexpected error: %v", err)
|
||||
logrus.Fatalf("Unexpected error: %v", err)
|
||||
} else {
|
||||
fmt.Println("Database setup completed succesfully")
|
||||
logrus.Info("Database setup completed succesfully")
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
Reference in New Issue
Block a user