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:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import { Connection, PublicKey } from '@solana/web3.js'
|
||||
import { logger } from '../utils/logger'
|
||||
import { PriceServiceConnection } from '@pythnetwork/price-service-client'
|
||||
import { getMarketConfig } from '../../config/trading'
|
||||
|
||||
@@ -47,7 +48,7 @@ export class PythPriceMonitor {
|
||||
},
|
||||
})
|
||||
|
||||
console.log('✅ Pyth price monitor created')
|
||||
logger.log('✅ Pyth price monitor created')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +60,7 @@ export class PythPriceMonitor {
|
||||
return
|
||||
}
|
||||
|
||||
console.log('🚀 Starting Pyth price monitor for:', config.symbols)
|
||||
logger.log('🚀 Starting Pyth price monitor for:', config.symbols)
|
||||
|
||||
try {
|
||||
// Get Pyth price feed IDs for all symbols
|
||||
@@ -68,7 +69,7 @@ export class PythPriceMonitor {
|
||||
return marketConfig.pythPriceFeedId
|
||||
})
|
||||
|
||||
console.log('📡 Subscribing to Pyth WebSocket...')
|
||||
logger.log('📡 Subscribing to Pyth WebSocket...')
|
||||
|
||||
// Subscribe to Pyth WebSocket for real-time updates
|
||||
this.priceService.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
|
||||
@@ -112,13 +113,13 @@ export class PythPriceMonitor {
|
||||
}
|
||||
})
|
||||
|
||||
console.log('✅ Pyth WebSocket subscribed')
|
||||
logger.log('✅ Pyth WebSocket subscribed')
|
||||
|
||||
// Start polling fallback (every 2 seconds) in case WebSocket fails
|
||||
this.startPollingFallback(config)
|
||||
|
||||
this.isMonitoring = true
|
||||
console.log('✅ Price monitoring active')
|
||||
logger.log('✅ Price monitoring active')
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to start price monitor:', error)
|
||||
@@ -130,7 +131,7 @@ export class PythPriceMonitor {
|
||||
* Polling fallback - checks prices every 2 seconds via RPC
|
||||
*/
|
||||
private startPollingFallback(config: PriceMonitorConfig): void {
|
||||
console.log('🔄 Starting polling fallback (every 2s)...')
|
||||
logger.log('🔄 Starting polling fallback (every 2s)...')
|
||||
|
||||
for (const symbol of config.symbols) {
|
||||
const interval = setInterval(async () => {
|
||||
@@ -140,7 +141,7 @@ export class PythPriceMonitor {
|
||||
const timeSinceUpdate = Date.now() - lastUpdate
|
||||
|
||||
if (timeSinceUpdate > 5000) {
|
||||
console.log(`⚠️ WebSocket stale for ${symbol}, using polling fallback`)
|
||||
logger.log(`⚠️ WebSocket stale for ${symbol}, using polling fallback`)
|
||||
await this.fetchPriceViaRPC(symbol, config.onPriceUpdate)
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -154,7 +155,7 @@ export class PythPriceMonitor {
|
||||
this.pollingIntervals.set(symbol, interval)
|
||||
}
|
||||
|
||||
console.log('✅ Polling fallback active')
|
||||
logger.log('✅ Polling fallback active')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,7 +224,7 @@ export class PythPriceMonitor {
|
||||
return
|
||||
}
|
||||
|
||||
console.log('🛑 Stopping price monitor...')
|
||||
logger.log('🛑 Stopping price monitor...')
|
||||
|
||||
// Clear polling intervals
|
||||
this.pollingIntervals.forEach(interval => clearInterval(interval))
|
||||
@@ -237,7 +238,7 @@ export class PythPriceMonitor {
|
||||
this.lastUpdateTime.clear()
|
||||
this.isMonitoring = false
|
||||
|
||||
console.log('✅ Price monitor stopped')
|
||||
logger.log('✅ Price monitor stopped')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user