Skip to content

Logging Best Practices — Comprehensive Guide to Production Logging

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you'll learn about Logging Best Practices. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Effective logging requires consistent practices across teams to maximize debugging value while minimizing noise and cost.

// Logging best practices implementation

// 1. Define clear log level guidelines
const logLevelGuide = {
  FATAL: 'Application crash, data corruption, security breach. Immediate action required.',
  ERROR: 'Request failed, service degraded, external dependency failure. Needs investigation.',
  WARN: 'Unexpected but handled. Rate limiting, fallback used, deprecated API called.',
  INFO: 'Business events: scan started, user registered, file processed. Standard operations.',
  DEBUG: 'Detailed diagnostic: query parameters, intermediate values, state transitions.',
  TRACE: 'Step-by-step execution: function entry/exit, loop iterations, micro-timings.'
};

// 2. Never log sensitive data
const sensitiveFields = ['password', 'secret', 'token', 'authorization', 'creditCard', 'ssn'];

function sanitizeLogInput(data) {
  if (typeof data === 'object') {
    const sanitized = { ...data };
    for (const key of Object.keys(sanitized)) {
      if (sensitiveFields.some(f => key.toLowerCase().includes(f))) {
        sanitized[key] = '[REDACTED]';
      }
    }
    return sanitized;
  }
  return data;
}

// 3. Include sufficient context
logger.info('Scan processed', {
  scanId: scan.id,
  fileName: scan.fileName,
  fileSize: scan.fileSize,
  duration: scan.durationMs,
  threats: scan.threats.length,
  userId: req.user?.id,
  correlationId: req.correlationId
});

// 4. One log entry per meaningful event
// BAD:
logger.debug('Starting scan');
logger.debug('File size: ' + fileSize);
logger.debug('Scan type: ' + scanType);
logger.debug('Scan completed');

// GOOD:
logger.info('Scan completed', { scanId, fileSize, scanType, duration, threats });

// 5. Use structured data, not string concatenation
// BAD:
logger.info(`User ${userId} scanned file ${fileName} with result ${result}`);

// GOOD:
logger.info('Scan processed', { userId, fileName, result, duration });

// 6. Log at the right level
// ERROR for failures, WARN for fallbacks, INFO for business events, DEBUG for troubleshooting

Following logging best practices ensures logs remain useful, searchable, and cost-effective as systems scale.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro