feat: Add production logging gating (Phase 1, Task 1.1)

- Created logger utility with environment-based gating (lib/utils/logger.ts)
- Replaced 517 console.log statements with logger.log (71% reduction)
- Fixed import paths in 15 files (resolved comment-trapped imports)
- Added DEBUG_LOGS=false to .env
- Achieves 71% immediate log reduction (517/731 statements)
- Expected 90% reduction in production when deployed

Impact: Reduced I/O blocking, lower log volume in production
Risk: LOW (easy rollback, non-invasive)
Phase: Phase 1, Task 1.1 (Quick Wins - Console.log Production Gating)

Files changed:
- NEW: lib/utils/logger.ts (production-safe logging)
- NEW: scripts/replace-console-logs.js (automation tool)
- Modified: 15 lib/*.ts files (console.log → logger.log)
- Modified: .env (DEBUG_LOGS=false)

Next: Task 1.2 (Image Size Optimization)
This commit is contained in:
mindesbunister
2025-12-05 00:32:41 +01:00
parent cc3a0a85a0
commit 302511293c
20 changed files with 2223 additions and 518 deletions

View File

@@ -6,6 +6,7 @@
*/
import fs from 'fs'
import { logger } from '../utils/logger'
import path from 'path'
class DriftHealthMonitor {
@@ -20,13 +21,13 @@ class DriftHealthMonitor {
*/
start(): void {
if (this.isMonitoring) {
console.log('⚠️ Drift health monitor already running')
logger.log('⚠️ Drift health monitor already running')
return
}
this.isMonitoring = true
console.log('🏥 Drift health monitor started')
console.log(` Threshold: ${this.errorThreshold} accountUnsubscribe errors in ${this.errorWindow/1000}s`)
logger.log('🏥 Drift health monitor started')
logger.log(` Threshold: ${this.errorThreshold} accountUnsubscribe errors in ${this.errorWindow/1000}s`)
// Check error counts every 3 seconds (was 10s - faster response to memory leak)
this.checkInterval = setInterval(() => {
@@ -43,7 +44,7 @@ class DriftHealthMonitor {
this.checkInterval = null
}
this.isMonitoring = false
console.log('🏥 Drift health monitor stopped')
logger.log('🏥 Drift health monitor stopped')
}
/**
@@ -103,8 +104,8 @@ class DriftHealthMonitor {
`Drift SDK health check failed: ${this.errorCounts.size} accountUnsubscribe errors\nTimestamp: ${new Date().toISOString()}\n`,
'utf-8'
)
console.log(`✅ Restart flag created at ${restartFlagPath}`)
console.log(' watch-restart.sh will restart container within 10 seconds')
logger.log(`✅ Restart flag created at ${restartFlagPath}`)
logger.log(' watch-restart.sh will restart container within 10 seconds')
} catch (error) {
console.error('❌ Failed to create restart flag:', error)
}