Skip to content
Learni
View all tutorials
DevOps

How to Set Up Robust Log Aggregation in 2026

12 minBEGINNER
Lire en français

Introduction

Log aggregation involves collecting event logs from various sources and centralizing them. This simplifies debugging and application monitoring. In 2026, simple tools like Winston enable quick implementation even for beginners. This tutorial shows you how to implement a working solution in Node.js.

Prerequisites

  • Node.js 20+
  • Basic JavaScript knowledge
  • A code editor (VS Code recommended)

Project Initialization

terminal
mkdir log-aggregation-tutorial
cd log-aggregation-tutorial
npm init -y
npm install winston

This command creates the project folder and installs Winston, the most widely used logging library for Node.js. It enables easy generation of structured logs.

Logger Configuration

logger.js
const winston = require('winston');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.File({ filename: 'logs/combined.log' }),
    new winston.transports.Console()
  ]
});

module.exports = logger;

This file configures Winston to write logs in JSON format to a combined file and display them in the console. The info level captures important messages.

Usage in the Application

app.js
const logger = require('./logger');

logger.info('Application démarrée');
logger.error('Une erreur est survenue', { error: 'Exemple' });

console.log('Logs générés avec succès');

The application imports the logger and emits logs at different levels. Logs are automatically written to the combined file for aggregation.

Simple Aggregation Script

aggregate.js
const fs = require('fs');
const path = require('path');

const logDir = path.join(__dirname, 'logs');
const outputFile = path.join(__dirname, 'aggregated.log');

let aggregated = '';

fs.readdirSync(logDir).forEach(file => {
  if (file.endsWith('.log')) {
    const content = fs.readFileSync(path.join(logDir, file), 'utf8');
    aggregated += content + '\n';
  }
});

fs.writeFileSync(outputFile, aggregated);
console.log('Logs agrégés dans aggregated.log');

This script reads all log files from the directory and concatenates them into a single file. It provides basic aggregation without external tools.

Execution and Testing

terminal
node app.js
node aggregate.js
cat aggregated.log

Run these commands to generate the logs and then aggregate them. The final file contains all centralized events.

Best Practices

  • Always structure logs in JSON to facilitate parsing
  • Use consistent log levels (info, warn, error)
  • Rotate log files to avoid excessively large files
  • Centralize logs in a dedicated directory
  • Add contextual metadata to logs

Common Mistakes to Avoid

  • Forgetting to create the logs directory before writing
  • Using unstructured logs (raw text)
  • Not handling exceptions in logging code
  • Ignoring file rotation which can fill up the disk

Going Further

Check out our Learni courses to deepen your knowledge of observability and advanced tools like Loki or ELK.