# 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. ## Install Save in your project root: # log4js — Flexible Logging Framework for Node.js ## Quick Use ```bash npm install log4js ``` ```js const log4js = require('log4js'); log4js.configure({ appenders: { out: { type: 'stdout' }, file: { type: 'file', filename: 'app.log' } }, categories: { default: { appenders: ['out', 'file'], level: 'info' } } }); const logger = log4js.getLogger(); logger.info('Application started'); logger.error('Something failed'); ``` ## 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 - https://github.com/log4js-node/log4js-node - https://log4js-node.github.io/log4js-node/ --- Source: https://tokrepo.com/en/workflows/asset-e93695fe Author: AI Open Source