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:
51
lib/utils/logger.ts
Normal file
51
lib/utils/logger.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Production-safe logging utility
|
||||
* Automatically gates console.log based on environment
|
||||
* Always allows console.error for critical issues
|
||||
*/
|
||||
|
||||
const isDev = process.env.NODE_ENV !== 'production'
|
||||
const debugEnabled = process.env.DEBUG_LOGS === 'true'
|
||||
|
||||
export const logger = {
|
||||
/**
|
||||
* Debug logging - only in development or when DEBUG_LOGS=true
|
||||
*/
|
||||
log: (...args: any[]) => {
|
||||
if (isDev || debugEnabled) {
|
||||
console.log(...args)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Error logging - always enabled (critical for production issues)
|
||||
*/
|
||||
error: (...args: any[]) => {
|
||||
console.error(...args)
|
||||
},
|
||||
|
||||
/**
|
||||
* Warning logging - always enabled (important for production monitoring)
|
||||
*/
|
||||
warn: (...args: any[]) => {
|
||||
console.warn(...args)
|
||||
},
|
||||
|
||||
/**
|
||||
* Info logging - only in development or when DEBUG_LOGS=true
|
||||
*/
|
||||
info: (...args: any[]) => {
|
||||
if (isDev || debugEnabled) {
|
||||
console.info(...args)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Debug-specific logging - only when DEBUG_LOGS=true
|
||||
*/
|
||||
debug: (...args: any[]) => {
|
||||
if (debugEnabled) {
|
||||
console.log('[DEBUG]', ...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import * as fs from 'fs'
|
||||
import { logger } from '../utils/logger'
|
||||
import * as path from 'path'
|
||||
|
||||
const LOG_DIR = '/app/logs'
|
||||
@@ -31,7 +32,7 @@ function rotateLogIfNeeded(logPath: string) {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
|
||||
const rotatedPath = `${logPath}.${timestamp}`
|
||||
fs.renameSync(logPath, rotatedPath)
|
||||
console.log(`📦 Rotated log: ${rotatedPath}`)
|
||||
logger.log(`📦 Rotated log: ${rotatedPath}`)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -86,7 +87,7 @@ export function logTradeExecution(
|
||||
: `Trade failed: ${tradeDetails.symbol} ${tradeDetails.direction} - ${tradeDetails.error}`
|
||||
|
||||
const entry = formatLogEntry(level, message, tradeDetails)
|
||||
console.log(success ? '✅' : '❌', message)
|
||||
logger.log(success ? '✅' : '❌', message)
|
||||
appendToLog(TRADE_LOG, entry)
|
||||
}
|
||||
|
||||
@@ -109,7 +110,7 @@ export function logDatabaseOperation(
|
||||
: `${operation} failed: ${details.error?.message || 'Unknown error'}`
|
||||
|
||||
const entry = formatLogEntry(level, message, details)
|
||||
console.log(success ? '💾' : '❌', message)
|
||||
logger.log(success ? '💾' : '❌', message)
|
||||
appendToLog(ERROR_LOG, entry)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user