✨ Enhance logging configuration by introducing file output options and rotation settings in config.dev.toml. Update logger implementation to support multiple output streams, including file logging with rotation using lumberjack. Improve error handling and logging across various components, ensuring consistent logging practices. Update .gitignore to include log files and compressed logs. Add new Go module dependency for lumberjack.
This commit is contained in:
@@ -7,32 +7,48 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ProvideDB creates a PostgreSQL connection pool
|
||||
func ProvideDB(cfg *config.Config) (*pgxpool.Pool, error) {
|
||||
func ProvideDB(cfg *config.Config, logger *zap.Logger) (*pgxpool.Pool, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
poolConfig, err := pgxpool.ParseConfig(cfg.Postgres.URL)
|
||||
if err != nil {
|
||||
logger.Error("failed to parse postgres URL",
|
||||
zap.String("url", cfg.Postgres.URL),
|
||||
zap.Error(err),
|
||||
)
|
||||
return nil, fmt.Errorf("failed to parse postgres URL: %w", err)
|
||||
}
|
||||
|
||||
|
||||
poolConfig.MaxConns = int32(cfg.Postgres.MaxConns)
|
||||
poolConfig.MinConns = int32(cfg.Postgres.MinConns)
|
||||
poolConfig.MaxConnLifetime = time.Hour
|
||||
poolConfig.MaxConnIdleTime = time.Minute * 30
|
||||
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||||
if err != nil {
|
||||
logger.Error("failed to create connection pool",
|
||||
zap.String("url", cfg.Postgres.URL),
|
||||
zap.Int32("max_conns", cfg.Postgres.MaxConns),
|
||||
zap.Int32("min_conns", cfg.Postgres.MinConns),
|
||||
zap.Error(err),
|
||||
)
|
||||
return nil, fmt.Errorf("failed to create connection pool: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Test connection
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
logger.Error("failed to ping database",
|
||||
zap.String("url", cfg.Postgres.URL),
|
||||
zap.Error(err),
|
||||
)
|
||||
return nil, fmt.Errorf("failed to ping database: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Log the connection pool
|
||||
logger.Info("Connected to PostgreSQL", zap.Int32("max_conns", cfg.Postgres.MaxConns), zap.Int32("min_conns", cfg.Postgres.MinConns), zap.String("url", cfg.Postgres.URL))
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user