Why logging every HTTP request is the fastest way to bankrupt your startup.
When you build a small app, you print everything to the console: "User logged in", "Database queried", "Button clicked". When your app goes viral, you are suddenly processing 10,000 requests per second. Every single log line is sent over the network to a central log server (like Datadog or Splunk). Very quickly, the network is choked, the log server runs out of disk space, and you get a bill for $50,000 at the end of the month. To survive, you must aggressively filter and sample your logs before they leave your servers.
Managing log volume requires strict discipline at the application layer:
DEBUG and INFO logs. Only emit WARN and ERROR. The vast majority of system noise disappears.// 1. Strict Log Levels
const logger = createLogger({
// Only print Warnings and Errors in Production!
level: process.env.NODE_ENV === 'prod' ? 'warn' : 'debug'
});
// 2. Head-based Sampling (1% of traffic)
function logHttpRequest(req, res) {
if (Math.random() < 0.01) {
logger.info(`Access: ${req.url} - ${res.statusCode}`);
}
}
// 3. Log Aggregation (Batching)
// Never send 1 log at a time over HTTP. Buffer them in memory
// and send 500 at once to save network overhead.
Sampling means you intentionally throw away data. If a customer reports a bug ("My payment failed at 2:00 PM"), and that specific request wasn't part of the 1% sample, you have no logs to investigate. The trade-off is between perfect observability (astronomical cost) and statistical observability (affordable, but missing needles in the haystack).