Introduction to Log4js
Log4js is a versatile logging library for Node.js applications, providing a variety of powerful features and configuration options to handle logging effectively. This library supports multiple appenders, log levels, and even custom layouts, making it a go-to solution for developers aiming to implement robust logging strategies in their applications.
Basic Setup
To get started with Log4js, you need to install it via npm:
npm install log4js
Then, include Log4js in your Node.js application:
const log4js = require('log4js');
Creating a Simple Logger
Here’s how you can create a simple logger:
log4js.configure({ appenders: { out: { type: 'stdout' } }, categories: { default: { appenders: ['out'], level: 'info' } } }); const logger = log4js.getLogger(); logger.info('This is an informational message');
Using Different Log Levels
Log4js supports different log levels. You can log messages as debug, info, warn, error, or fatal:
logger.debug('This is a debug message'); logger.info('This is an info message'); logger.warn('This is a warning message'); logger.error('This is an error message'); logger.fatal('This is a fatal message');
File Logging
To log messages to a file, configure the file appender:
log4js.configure({ appenders: { file: { type: 'file', filename: 'app.log' } }, categories: { default: { appenders: ['file'], level: 'info' } } }); const fileLogger = log4js.getLogger('file'); fileLogger.info('Logging to a file');
Using Multiple Appenders
You can use multiple appenders to log messages to different destinations:
log4js.configure({ appenders: { out: { type: 'stdout' }, file: { type: 'file', filename: 'app.log' } }, categories: { default: { appenders: ['out', 'file'], level: 'info' } } }); const multiLogger = log4js.getLogger(); multiLogger.info('Logging to stdout and file');
Custom Log Layouts
Log4js allows you to define custom layouts for your log messages:
log4js.configure({ appenders: { custom: { type: 'file', filename: 'custom.log', layout: { type: 'pattern', pattern: '%d %p %c %m%n' } } }, categories: { default: { appenders: ['custom'], level: 'info' } } }); const customLogger = log4js.getLogger('custom'); customLogger.info('This is an info message with a custom layout');
Creating an Application with Log4js
Let’s put it all together in a sample application:
const express = require('express'); const log4js = require('log4js'); log4js.configure({ appenders: { out: { type: 'stdout' }, app: { type: 'file', filename: 'application.log' } }, categories: { default: { appenders: ['out', 'app'], level: 'debug' } } }); const app = express(); const logger = log4js.getLogger(); app.use((req, res, next) => { logger.debug(`Received request for ${req.url}`); next(); }); app.get('/', (req, res) => { logger.info('Root endpoint hit'); res.send('Hello World'); }); app.use((err, req, res, next) => { logger.error('An error occurred', err); res.status(500).send('Internal Server Error'); }); app.listen(3000, () => { logger.info('Application started on port 3000'); });
With these configurations and examples, you can efficiently incorporate Log4js into your Node.js applications to handle complex logging requirements with ease.
Hash: 648960f09f0520c432b1a11cd7b1ec6cb50b6bd3eebb5c8244827c5cb7250a38