fix(logger): address PR #19 code review feedback

Fixes:
- Remove unused import logBufferMetrics from event-buffer.ts
- Add safeStringify() for robust data formatting:
  - Handles circular references gracefully
  - Formats Error objects properly
  - Truncates large arrays [Array(n)]
  - Handles undefined/null/Date/BigInt values
- Add getMode() method to AdaptiveTimeoutCalculator to avoid
  duplicating mode calculation logic in flush()
- Fix empty data object handling (no trailing space)

Tests:
- Add comprehensive test suite (39 tests) covering:
  - Environment variable check (BAILEYS_LOG=false)
  - All logging functions with emoji verification
  - Session name handling
  - formatLogData edge cases (circular, Error, arrays, etc.)
This commit is contained in:
Claude
2026-01-20 21:44:08 +00:00
parent 3f2ced4501
commit bb73662e8f
3 changed files with 398 additions and 22 deletions
+16 -12
View File
@@ -15,7 +15,7 @@ import { trimUndefined } from './generics'
import type { ILogger } from './logger'
import { updateMessageWithReaction, updateMessageWithReceipt } from './messages'
import { isRealMessage, shouldIncrementChatUnread } from './process-message'
import { logEventBuffer, logBufferMetrics } from './baileys-logger'
import { logEventBuffer } from './baileys-logger'
// ============================================================================
// BUFFER CONFIGURATION - Environment Variable Support
@@ -316,6 +316,19 @@ class AdaptiveTimeoutCalculator {
this.eventTimestamps = []
this.currentTimeout = (this.minTimeout + this.maxTimeout) / 2
}
/**
* Get current adaptive mode based on timeout
* Returns 'aggressive', 'balanced', or 'conservative'
*/
getMode(): 'aggressive' | 'balanced' | 'conservative' {
if (this.currentTimeout <= this.minTimeout * 1.5) {
return 'aggressive'
} else if (this.currentTimeout >= this.maxTimeout * 0.8) {
return 'conservative'
}
return 'balanced'
}
}
// ============================================================================
@@ -542,21 +555,12 @@ export const makeEventBuffer = (
// Record metrics
recordFlushMetrics(eventCount, force)
// Determine adaptive mode label
const currentTimeoutMs = config.enableAdaptiveTimeout ? adaptiveTimeout.getTimeout() : config.bufferTimeoutMs
let adaptiveMode = 'balanced'
if (currentTimeoutMs <= config.minBufferTimeoutMs * 1.5) {
adaptiveMode = 'aggressive'
} else if (currentTimeoutMs >= config.maxBufferTimeoutMs * 0.8) {
adaptiveMode = 'conservative'
}
// Log with [BAILEYS] prefix
// Log with [BAILEYS] prefix - use getMode() to avoid duplicating mode calculation logic
const flushDuration = Date.now() - flushStartTime
logEventBuffer('buffer_flush', {
flushCount: stats.totalFlushes,
historyCacheSize: stats.historyCacheSize,
mode: adaptiveMode,
mode: config.enableAdaptiveTimeout ? adaptiveTimeout.getMode() : 'fixed',
...(flushDuration > 5 ? { duration: `${flushDuration}ms` } : {})
})