Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsJul 4, 2026·3 min de lecture

log4js — Flexible Logging Framework for Node.js

A logging framework for Node.js inspired by log4j. Provides configurable appenders, categories, and log levels for routing log output to files, consoles, network endpoints, and more.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
log4js Overview
Commande d'installation directe
npx -y tokrepo@latest install e93695fe-773f-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

log4js is a logging framework for Node.js modeled after the Java log4j library. It provides a familiar category-and-appender architecture where log messages are routed to one or more destinations based on their category and level. This makes it straightforward to separate debug, info, and error output across different files or services.

What log4js Does

  • Routes log messages to multiple appenders (stdout, files, rotating files, network, custom)
  • Organizes loggers into hierarchical categories with independent level thresholds
  • Supports automatic log file rotation by size or date
  • Provides layout formatters for controlling log line structure (pattern, JSON, colored)
  • Offers built-in appenders for syslog, SMTP, TCP, and multiprocess logging

Architecture Overview

log4js uses a configuration-driven approach. A central configure() call defines appenders (where logs go) and categories (which loggers use which appenders at what level). When code calls getLogger('category'), log4js returns a logger bound to that category. Each log call checks the category's level threshold, formats the message using the appender's layout, and writes to all assigned appenders. The hierarchical category system means a logger for 'app.db' inherits settings from 'app' if not explicitly configured.

Self-Hosting & Configuration

  • Install via npm; configure appenders and categories via log4js.configure()
  • Use the dateFile appender for daily rotating log files with automatic cleanup
  • Configure different log levels per category to control verbosity granularly
  • Load configuration from a JSON file or pass an object directly
  • Integrate with Express via the connectLogger middleware for HTTP request logging

Key Features

  • Multiple built-in appenders: stdout, stderr, file, dateFile, TCP, multiprocess, and more
  • Hierarchical category system for per-module log level control
  • Configurable layouts: basic, colored, pattern-based, and JSON
  • Log file rotation by date or size with configurable retention
  • Connect/Express middleware for automatic HTTP request logging

Comparison with Similar Tools

  • Winston — transport-based logging with a wider ecosystem; more flexible but heavier configuration
  • Pino — performance-focused JSON logger; faster but fewer built-in output destinations
  • Bunyan — structured JSON logging with child loggers; simpler API but less routing flexibility
  • console.log — zero setup but no levels, categories, file output, or rotation

FAQ

Q: How do I rotate log files? A: Use the dateFile appender with a pattern like yyyy-MM-dd and set daysToKeep to control retention. The file appender supports maxLogSize for size-based rotation.

Q: Can I use different log levels for different modules? A: Yes. Define separate categories in the configuration, each with its own level. Use getLogger('moduleName') to get the appropriate logger.

Q: Does log4js support JSON output? A: Yes. Set the layout type to 'json' in the appender configuration to output structured JSON log lines.

Q: How do I log HTTP requests in Express? A: Use app.use(log4js.connectLogger(logger, { level: 'info' })) to log each incoming request with method, URL, status, and response time.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires