log4js
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
date-format | 4.0.14 | 5.6 kB | MIT | prod | |
debug | 4.3.4 | 12.94 kB | MIT | prod | |
flatted | 3.3.1 | 39.32 kB | ISC | prod | |
rfdc | 1.3.1 | 6.04 kB | MIT | prod | |
streamroller | 3.1.5 | 11.71 kB | MIT | prod |
Log4js is a popular npm package specifically designed to work with Node.js. It's a port of log4js, a comprehensive logging library, fine-tuned to work seamlessly with Node.js applications. Log4js allows developers to generate log files, providing colored console logging to stdout or stderr, file appender with log rolling based on file size or dates, logger for Connect/Express servers, configurable log message layouts/patterns, and the ability to set different log levels for different log categories. Additionally, it supports several optional appenders such as SMTP, GELF, Loggy, Logstash, logFaces, RabbitMQ, Redis, Hipchat, Slack, mailgun, and InfluxDB.
To utilize Log4js in your JavaScript/Node.js projects, you will first have to install it via NPM using the command npm install log4js
. Then, require it in your project file using var log4js = require("log4js");
. A logger is then created with var logger = log4js.getLogger();
. You can set the log level with logger.level = "debug";
(other levels are TRACE, INFO, WARN, ERROR, and FATAL), and finally, you can log a message with logger.debug("Some debug messages");
.
For more complex usage, Log4js can be configured with different appenders and categories. For example:
const log4js = require("log4js");
log4js.configure({
appenders: { cheese: { type: "file", filename: "cheese.log" } },
categories: { default: { appenders: ["cheese"], level: "error" } },
});
const logger = log4js.getLogger("cheese");
logger.trace("Entering cheese testing");
logger.debug("Got cheese.");
logger.info("Cheese is Comté.");
logger.warn("Cheese is quite smelly.");
logger.error("Cheese is too ripe!");
logger.fatal("Cheese was breeding ground for listeria.");
In this example, the logger outputs to a file named 'cheese.log'.
The full documentation for log4js can be found on the official log4js-node website. Additional information including migration guides for different versions and an example application can also be found on the website. The website provides details about the installation, usage, configuration, and the entire features set of Log4js.