{"library":"log4js","title":"Log4js for Node.js","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install log4js"],"cli":null},"imports":["import pkg from 'log4js';\nconst log4js = pkg;","import pkg from 'log4js';\nconst { getLogger } = pkg;\nconst logger = getLogger();","import type { Configuration, Logger } from 'log4js';","const log4js = require('log4js');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import pkg from 'log4js';\nconst { configure, getLogger, shutdown } = pkg;\n\n// Configure log4js with a console and a file appender\nconfigure({\n  appenders: {\n    console: { type: 'console' },\n    fileAppender: {\n      type: 'file',\n      filename: 'application.log',\n      maxLogSize: 10485760, // 10MB\n      backups: 3,\n      compress: true\n    }\n  },\n  categories: {\n    default: { appenders: ['console'], level: 'info' },\n    app: { appenders: ['console', 'fileAppender'], level: 'debug' }\n  }\n});\n\nconst logger = getLogger('app');\n\nlogger.trace('Entering application module.');\nlogger.debug('This is a debug message for the app category.');\nlogger.info('Application started successfully.');\nlogger.warn('A non-critical warning occurred.');\nlogger.error('An error happened: Something went wrong!');\nlogger.fatal('Fatal error: Application is shutting down.');\n\n// Important: Call shutdown on application exit to ensure all logs are written.\nprocess.on('SIGINT', () => {\n  console.log('Caught interrupt signal, shutting down log4js...');\n  shutdown(() => {\n    console.log('Log4js shutdown complete. Exiting.');\n    process.exit(0);\n  });\n});\n\n// Simulate some async operation for logs to process\nsetTimeout(() => {\n  logger.info('Simulated operation finished.');\n}, 500);","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}