✨ 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
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"caatsm/internal/adapter/mapper"
|
||||
"caatsm/internal/adapter/dto"
|
||||
"caatsm/internal/port"
|
||||
"caatsm/internal/adapter/mapper"
|
||||
obsmetrics "caatsm/internal/infra/metrics"
|
||||
"caatsm/internal/port"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -49,6 +49,11 @@ func (r *Repository) InsertOne(ctx context.Context, msg *dto.ParsedTelegram) err
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
r.logger.Error("failed to check existing message",
|
||||
zap.String("message_id", msg.MessageID),
|
||||
zap.String("date_time", msg.DateTime),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("failed to check existing message: %w", err)
|
||||
}
|
||||
if exists {
|
||||
@@ -64,6 +69,8 @@ func (r *Repository) InsertOne(ctx context.Context, msg *dto.ParsedTelegram) err
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
|
||||
r.logger.Error("failed to map message to DB row", zap.Error(err))
|
||||
return fmt.Errorf("failed to map message to DB row: %w", err)
|
||||
}
|
||||
|
||||
@@ -92,6 +99,12 @@ func (r *Repository) InsertOne(ctx context.Context, msg *dto.ParsedTelegram) err
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
obsmetrics.RecordDBQuery("insert_one", result, elapsed)
|
||||
r.logger.Error("failed to insert message",
|
||||
zap.String("uuid", msg.Uuid),
|
||||
zap.String("message_id", msg.MessageID),
|
||||
zap.Duration("elapsed", elapsed),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("failed to insert message: %w", err)
|
||||
}
|
||||
|
||||
@@ -128,6 +141,12 @@ func (r *Repository) InsertBatch(ctx context.Context, msgs []*dto.ParsedTelegram
|
||||
for i, msg := range msgs {
|
||||
row, err := r.mapper.ToDBRow(msg)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to map message to DB row in batch",
|
||||
zap.Int("index", i),
|
||||
zap.Int("total", len(msgs)),
|
||||
zap.String("message_id", msg.MessageID),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("failed to map message %d to DB row: %w", i, err)
|
||||
}
|
||||
rows[i] = row
|
||||
@@ -154,6 +173,11 @@ func (r *Repository) InsertBatch(ctx context.Context, msgs []*dto.ParsedTelegram
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
obsmetrics.RecordDBQuery("insert_batch", result, elapsed)
|
||||
r.logger.Error("failed to batch insert messages",
|
||||
zap.Int("attempted", len(msgs)),
|
||||
zap.Duration("elapsed", elapsed),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("failed to batch insert messages: %w", err)
|
||||
}
|
||||
|
||||
@@ -178,6 +202,7 @@ func (r *Repository) InsertRaw(ctx context.Context, msg *dto.ParsedTelegram) err
|
||||
err := fmt.Errorf("message is nil")
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
r.logger.Error("message is nil in InsertRaw")
|
||||
return fmt.Errorf("message is nil")
|
||||
}
|
||||
if msg.Uuid == "" {
|
||||
@@ -194,6 +219,10 @@ func (r *Repository) InsertRaw(ctx context.Context, msg *dto.ParsedTelegram) err
|
||||
}
|
||||
metadataJSON, err := json.Marshal(metadata)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to marshal metadata",
|
||||
zap.String("uuid", msg.Uuid),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("failed to marshal metadata: %w", err)
|
||||
}
|
||||
|
||||
@@ -227,6 +256,12 @@ func (r *Repository) InsertRaw(ctx context.Context, msg *dto.ParsedTelegram) err
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
obsmetrics.RecordDBQuery("insert_raw", result, elapsed)
|
||||
r.logger.Error("failed to insert raw telegram",
|
||||
zap.String("uuid", msg.Uuid),
|
||||
zap.String("status", string(msg.Status)),
|
||||
zap.Duration("elapsed", elapsed),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("failed to insert raw telegram: %w", err)
|
||||
}
|
||||
|
||||
@@ -261,6 +296,11 @@ func (r *Repository) messageExists(ctx context.Context, messageID, dateTime stri
|
||||
if err == pgx.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
r.logger.Error("failed to check message existence",
|
||||
zap.String("message_id", messageID),
|
||||
zap.String("date_time", dateTime),
|
||||
zap.Error(err),
|
||||
)
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user