refactor(logging): add TruncateURL helper and fix magic numbers
Extract URL truncation to logging package, replace magic numbers with named constants for linter compliance.
- commit
- 74af521655e0f6760f6b9053672c33aaa7d580d0
- author
- brice · · 2026-01-14 08:45 UTC
- parent
- 8d4361b
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
3 files · +16 -3
M internal/application/usecase/navigate.go +5 -2
| @@ -17,6 +17,9 @@ const ( | |||
| 17 | 17 | // historyQueueSize is the buffer size for the async history queue. | |
| 18 | 18 | // If the queue is full, new records are dropped with a warning. | |
| 19 | 19 | historyQueueSize = 100 | |
| 20 | + | ||
| 21 | + | // logURLMaxLen is the max length for URLs in log messages. | |
| 22 | + | logURLMaxLen = 60 | |
| 20 | 23 | ) | |
| 21 | 24 | ||
| 22 | 25 | // historyRecord holds data for async history recording. | |
| @@ -137,7 +140,7 @@ func (uc *NavigateUseCase) RecordHistory(ctx context.Context, url string) { | |||
| 137 | 140 | // Non-blocking send to async queue | |
| 138 | 141 | select { | |
| 139 | 142 | case uc.historyQueue <- historyRecord{url: url, timestamp: now}: | |
| 140 | - | log.Debug().Str("url", url).Msg("history record queued") | |
| 143 | + | log.Debug().Str("url", logging.TruncateURL(url, logURLMaxLen)).Msg("history record queued") | |
| 141 | 144 | default: | |
| 142 | 145 | // Queue full - log warning but don't block navigation | |
| 143 | 146 | log.Warn().Str("url", url).Msg("history queue full, dropping record") | |
| @@ -205,7 +208,7 @@ func (uc *NavigateUseCase) UpdateHistoryTitle(ctx context.Context, url, title st | |||
| 205 | 208 | ||
| 206 | 209 | // Normalize URL to match storage (e.g., github.com/ -> github.com) | |
| 207 | 210 | url = normalizeURLForHistory(url) | |
| 208 | - | log.Debug().Str("url", url).Str("title", title).Msg("updating history title") | |
| 211 | + | log.Debug().Str("url", logging.TruncateURL(url, logURLMaxLen)).Str("title", title).Msg("updating history title") | |
| 209 | 212 | ||
| 210 | 213 | entry, err := uc.historyRepo.FindByURL(ctx, url) | |
| 211 | 214 | if err != nil { | |
| @@ -17,6 +17,9 @@ const ( | |||||
| 17 | // historyQueueSize is the buffer size for the async history queue. | 17 | // historyQueueSize is the buffer size for the async history queue. | ||
| 18 | // If the queue is full, new records are dropped with a warning. | 18 | // If the queue is full, new records are dropped with a warning. | ||
| 19 | historyQueueSize = 100 | 19 | historyQueueSize = 100 | ||
| 20 | + | ||||
| 21 | + | // logURLMaxLen is the max length for URLs in log messages. | |||
| 22 | + | logURLMaxLen = 60 | |||
| 20 | ) | 23 | ) | ||
| 21 | 24 | ||||
| 22 | // historyRecord holds data for async history recording. | 25 | // historyRecord holds data for async history recording. | ||
| @@ -137,7 +140,7 @@ func (uc *NavigateUseCase) RecordHistory(ctx context.Context, url string) { | |||||
| 137 | // Non-blocking send to async queue | 140 | // Non-blocking send to async queue | ||
| 138 | select { | 141 | select { | ||
| 139 | case uc.historyQueue <- historyRecord{url: url, timestamp: now}: | 142 | case uc.historyQueue <- historyRecord{url: url, timestamp: now}: | ||
| 140 | - | log.Debug().Str("url", url).Msg("history record queued") | 143 | + | log.Debug().Str("url", logging.TruncateURL(url, logURLMaxLen)).Msg("history record queued") |
| 141 | default: | 144 | default: | ||
| 142 | // Queue full - log warning but don't block navigation | 145 | // Queue full - log warning but don't block navigation | ||
| 143 | log.Warn().Str("url", url).Msg("history queue full, dropping record") | 146 | log.Warn().Str("url", url).Msg("history queue full, dropping record") | ||
| @@ -205,7 +208,7 @@ func (uc *NavigateUseCase) UpdateHistoryTitle(ctx context.Context, url, title st | |||||
| 205 | 208 | ||||
| 206 | // Normalize URL to match storage (e.g., github.com/ -> github.com) | 209 | // Normalize URL to match storage (e.g., github.com/ -> github.com) | ||
| 207 | url = normalizeURLForHistory(url) | 210 | url = normalizeURLForHistory(url) | ||
| 208 | - | log.Debug().Str("url", url).Str("title", title).Msg("updating history title") | 211 | + | log.Debug().Str("url", logging.TruncateURL(url, logURLMaxLen)).Str("title", title).Msg("updating history title") |
| 209 | 212 | ||||
| 210 | entry, err := uc.historyRepo.FindByURL(ctx, url) | 213 | entry, err := uc.historyRepo.FindByURL(ctx, url) | ||
| 211 | if err != nil { | 214 | if err != nil { | ||
M internal/infrastructure/persistence/sqlite/history_repo.go +3 -1
| @@ -14,6 +14,8 @@ import ( | |||
| 14 | 14 | "github.com/bnema/dumber/internal/logging" | |
| 15 | 15 | ) | |
| 16 | 16 | ||
| 17 | + | const logURLMaxLen = 60 | |
| 18 | + | ||
| 17 | 19 | type historyRepo struct { | |
| 18 | 20 | queries *sqlc.Queries | |
| 19 | 21 | } | |
| @@ -25,7 +27,7 @@ func NewHistoryRepository(db *sql.DB) repository.HistoryRepository { | |||
| 25 | 27 | ||
| 26 | 28 | func (r *historyRepo) Save(ctx context.Context, entry *entity.HistoryEntry) error { | |
| 27 | 29 | log := logging.FromContext(ctx) | |
| 28 | - | log.Debug().Str("url", entry.URL).Msg("saving history entry") | |
| 30 | + | log.Debug().Str("url", logging.TruncateURL(entry.URL, logURLMaxLen)).Msg("saving history entry") | |
| 29 | 31 | ||
| 30 | 32 | return r.queries.UpsertHistory(ctx, sqlc.UpsertHistoryParams{ | |
| 31 | 33 | Url: entry.URL, | |
| @@ -14,6 +14,8 @@ import ( | |||||
| 14 | "github.com/bnema/dumber/internal/logging" | 14 | "github.com/bnema/dumber/internal/logging" | ||
| 15 | ) | 15 | ) | ||
| 16 | 16 | ||||
| 17 | + | const logURLMaxLen = 60 | |||
| 18 | + | ||||
| 17 | type historyRepo struct { | 19 | type historyRepo struct { | ||
| 18 | queries *sqlc.Queries | 20 | queries *sqlc.Queries | ||
| 19 | } | 21 | } | ||
| @@ -25,7 +27,7 @@ func NewHistoryRepository(db *sql.DB) repository.HistoryRepository { | |||||
| 25 | 27 | ||||
| 26 | func (r *historyRepo) Save(ctx context.Context, entry *entity.HistoryEntry) error { | 28 | func (r *historyRepo) Save(ctx context.Context, entry *entity.HistoryEntry) error { | ||
| 27 | log := logging.FromContext(ctx) | 29 | log := logging.FromContext(ctx) | ||
| 28 | - | log.Debug().Str("url", entry.URL).Msg("saving history entry") | 30 | + | log.Debug().Str("url", logging.TruncateURL(entry.URL, logURLMaxLen)).Msg("saving history entry") |
| 29 | 31 | ||||
| 30 | return r.queries.UpsertHistory(ctx, sqlc.UpsertHistoryParams{ | 32 | return r.queries.UpsertHistory(ctx, sqlc.UpsertHistoryParams{ | ||
| 31 | Url: entry.URL, | 33 | Url: entry.URL, | ||
M internal/logging/logger.go +8 -0
| @@ -189,3 +189,11 @@ func ParseLevel(level string) zerolog.Level { | |||
| 189 | 189 | return zerolog.InfoLevel | |
| 190 | 190 | } | |
| 191 | 191 | } | |
| 192 | + | ||
| 193 | + | // TruncateURL truncates a URL to maxLen characters for logging. | |
| 194 | + | func TruncateURL(url string, maxLen int) string { | |
| 195 | + | if len(url) <= maxLen { | |
| 196 | + | return url | |
| 197 | + | } | |
| 198 | + | return url[:maxLen] + "..." | |
| 199 | + | } | |
| @@ -189,3 +189,11 @@ func ParseLevel(level string) zerolog.Level { | |||||
| 189 | return zerolog.InfoLevel | 189 | return zerolog.InfoLevel | ||
| 190 | } | 190 | } | ||
| 191 | } | 191 | } | ||
| 192 | + | ||||
| 193 | + | // TruncateURL truncates a URL to maxLen characters for logging. | |||
| 194 | + | func TruncateURL(url string, maxLen int) string { | |||
| 195 | + | if len(url) <= maxLen { | |||
| 196 | + | return url | |||
| 197 | + | } | |||
| 198 | + | return url[:maxLen] + "..." | |||
| 199 | + | } | |||