Log4js is a comprehensive and configurable logging framework for Node.js, currently stable at version 6.9.1. It provides various appenders for outputting logs to the console, files (with rolling), and network services, alongside flexible layout patterns and category-based logging. While sharing a name with Java's Log4j, it behaves distinctly, making direct assumptions about parity a source of confusion. The project is actively maintained, with regular updates and a dedicated community. Key differentiators include its extensive appender support (many as optional, separate packages), highly customizable log levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL), and built-in TypeScript definitions, offering a robust solution for managing application logs in various environments.
npm install log4jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates configuring Log4js with both console and file appenders, setting different log levels for categories, and performing various log outputs. It also includes crucial `shutdown` handling for graceful application termination.
Review the migration guide and update your configuration object to include named appenders and define categories explicitly, especially the `default` category. All configuration must now pass through the `configure` function.
If you used any of these appenders, you must explicitly install their corresponding package (e.g., `npm i @log4js-node/smtp`) and update your configuration. Ensure your Node.js environment is at least version 6.
Manually adjust file permissions in your code or configuration if external access to log files is required. Refer to the `streamroller` documentation or `log4js` changelog for specifics.
Always set the desired log level either programmatically on the logger instance or declaratively within your `log4js.configure` object, especially for the `default` category.
Refer exclusively to the `log4js-node` documentation for usage and configuration. Do not apply concepts or configurations directly from Log4j (Java).
To replace `console` functions, you must now bind logger methods manually (e.g., `console.log = logger.info.bind(logger)`). For config reloading, integrate with an external file watcher library (like `watchr`) and manually call `log4js.shutdown()` followed by `log4js.configure()` again.
Add a `process.on('SIGINT', log4js.shutdown)` or similar handler to explicitly call `shutdown()` on application exit, especially for production environments.Use the CommonJS `require` syntax (`const log4js = require('log4js');`) or, if strictly in an ESM context, import the entire module as a default export and destructure (`import pkg from 'log4js'; const { configure, getLogger } = pkg;`).Update your `log4js.configure` object to match the v2.x schema, ensuring it defines named appenders within an `appenders` object and categories (including a `default` category) within a `categories` object.
Set the log level for your logger or a category to `debug`, `info`, `warn`, `error`, etc., either in your `log4js.configure` object (e.g., `categories: { default: { appenders: ['console'], level: 'info' } }`) or programmatically (`logger.level = 'debug';`).Install the correct npm package for the desired optional appender (e.g., `npm install @log4js-node/multi-file`) and ensure the `type` property in your configuration matches the appender's registered name.