ConfigsJul 4, 2026·3 min read

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.

Agent ready

Ready-to-run agent install

This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Established
Entrypoint
log4js Overview
Direct install command
npx -y tokrepo@latest install e93695fe-773f-11f1-9bc6-00163e2b0d79 --target codex

Run after dry-run confirms the install plan.

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

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets