Socket
Socket
Sign inDemoInstall

log4js

Package Overview
Dependencies
Maintainers
1
Versions
152
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

log4js

Port of Log4js to work with node.


Version published
Weekly downloads
4.4M
increased by5.14%
Maintainers
1
Weekly downloads
 
Created

What is log4js?

The log4js npm package is a logging library for Node.js, inspired by the Java-based Log4j. It provides a flexible logging system that can be configured to log to the console, to files, or to external logging services. It supports multiple log levels, categories, and appenders, allowing for fine-grained control over logging output.

What are log4js's main functionalities?

Basic Logging

This code sets up log4js to log messages to the standard output (console). It configures an appender named 'out' that writes to stdout and sets the default logging level to 'info'.

const log4js = require('log4js');
log4js.configure({
  appenders: { 'out': { type: 'stdout' } },
  categories: { default: { appenders: ['out'], level: 'info' } }
});
const logger = log4js.getLogger();
logger.info('Informational message');

File Appender

This code configures log4js to write log messages to a file named 'app.log' in the 'logs' directory. It creates a file appender and sets the logging level to 'warn'.

const log4js = require('log4js');
log4js.configure({
  appenders: { 'file': { type: 'file', filename: 'logs/app.log' } },
  categories: { default: { appenders: ['file'], level: 'info' } }
});
const logger = log4js.getLogger();
logger.warn('Warning message');

Log Levels

This code demonstrates the use of different log levels available in log4js. Each level represents a different severity of logging, with 'trace' being the least severe and 'fatal' being the most severe.

const log4js = require('log4js');
const logger = log4js.getLogger('myCategory');
logger.trace('Trace message');
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warn message');
logger.error('Error message');
logger.fatal('Fatal message');

Multiple Appenders and Categories

This code shows how to configure multiple appenders and categories. The 'default' category logs to both the console and a file, while the 'special' category logs only to a file and only logs messages at the 'error' level or higher.

const log4js = require('log4js');
log4js.configure({
  appenders: {
    'out': { type: 'stdout' },
    'file': { type: 'file', filename: 'logs/app.log' }
  },
  categories: {
    default: { appenders: ['out', 'file'], level: 'info' },
    special: { appenders: ['file'], level: 'error' }
  }
});
const defaultLogger = log4js.getLogger();
defaultLogger.info('Default category, logged to console and file');
const specialLogger = log4js.getLogger('special');
specialLogger.error('Special category, logged to file only');

Other packages similar to log4js

Keywords

FAQs

Package last updated on 22 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc