Log Sampling — Smart Log Sampling for High-Volume Systems
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you'll learn about Log Sampling. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Log sampling reduces storage and processing costs by recording only a representative subset of log events while preserving critical signals.
// Adaptive log sampler
class LogSampler {
constructor(options = {}) {
this.targetRate = options.targetRate || 0.1; // 10% sample
this.minRate = options.minRate || 0.01;
this.maxRate = options.maxRate || 1.0;
this.errorSampleRate = 1.0; // Always sample errors
this.sampleCounters = new Map();
this.windowSize = options.windowSize || 60000;
}
shouldSample(logEntry) {
// Always log errors
if (logEntry.level === 'error' || logEntry.level === 'fatal') {
return true;
}
// Always log security events
if (logEntry.type === 'security' || logEntry.type === 'audit') {
return true;
}
// Always log slow operations
if (logEntry.duration && logEntry.duration > 5000) {
return true;
}
// Probabilistic sampling for regular logs
const key = `${logEntry.service}:${logEntry.level}`;
const count = (this.sampleCounters.get(key) || 0) + 1;
this.sampleCounters.set(key, count);
// Adaptive rate based on volume
const rate = this.calculateAdaptiveRate(key, count);
return Math.random() < rate;
}
calculateAdaptiveRate(key, count) {
// Reduce sampling rate as volume increases
if (count > 10000) return Math.max(this.minRate, this.targetRate * 0.1);
if (count > 1000) return Math.max(this.minRate, this.targetRate * 0.5);
return this.targetRate;
}
resetCounters() {
this.sampleCounters.clear();
setTimeout(() => this.resetCounters(), this.windowSize);
}
}
// Usage
const sampler = new LogSampler({ targetRate: 0.05 });
app.use((req, res, next) => {
const logEntry = buildLogEntry(req);
if (sampler.shouldSample(logEntry)) {
logger.info(logEntry);
}
next();
});
Smart log sampling reduces log volume by 90-99% while maintaining visibility into errors, slow operations, and security events.
← Previous
Audit Logging — Implementing Immutable Audit Trails
Next →
Centralized Logging Architecture — Designing Log Infrastructure for Scale
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro