The Mystery of the Slow API
Our monitoring dashboard showed something strange: 40% slower response times during peak hours. The odd part? CPU and memory looked fine.
After days of profiling, we discovered the culprit:
console.log()
statements left in production.
Hereβs why theyβre dangerousβand how to fix them.
1. Why console.log()
is Worse Than You Think
π¨ Problem #1: Itβs Synchronous (Yes, Really!)
console.log('Logging data:', largeObject); // Blocks the event loop!
β
Proof:
console.time('sync');
for (let i = 0; i < 1e5; i++) console.log(i); // ~3 seconds!
console.timeEnd('sync');
π¨ Problem #2: Memory Leaks in Logging Libraries
Some logging frameworks buffer logs in memory before writing to disk/network.
-
Example: Unstructured
console.log
β uncontrolled growth in long-running processes.
π¨ Problem #3: Security Risks
console.log('User token:', req.headers.authorization); // π± Logs secrets!
2. How We Fixed It
β Switch to Async Logging (Winston/Pino)
// Before (Dangerous)
console.log(`User ${userId} logged in`);
// After (Safe)
const logger = require('pino')();
logger.info({ userId }, 'User logged in'); // Async + structured
Performance Boost:
| Logger | Req/sec | Latency (p99) |
|--------------|---------|--------------|
| console.log
| 8,000 | 120ms |
| Pino | 14,000 | 45ms |
β Use Log Levels (Avoid Spam in Prod)
if (process.env.NODE_ENV === 'development') {
console.log('Debug info'); // Only in dev
}
β Automatically Strip Logs in Production
# Use Babel/ESBuild to remove console.* in prod
esbuild app.js --drop:console
3. When console.log
is Actually Okay
β CLI tools (short-lived processes)
β Early prototyping (before proper logging is set up)
β Browser debugging (but use debugger
instead)
Key Takeaways
π console.log
blocks the event loop (yes, even in Node.js!)
π Uncontrolled logging tanks performance (we lost 40% throughput)
π Logs can leak secrets (check for sensitive data)
π Use Pino/Winston for async, structured logging
Further Reading
Have you been burned by logging in prod? Share your story!
Top comments (0)