Introduction to Genie Logger
Genie Logger is a versatile and powerful logging tool designed to help developers debug their applications efficiently and effectively. This tool offers a wide range of APIs that make logging a breeze, enabling developers to gain insights into their application’s behavior with minimal effort. Let’s explore some of the key features and APIs of Genie Logger, complete with code snippets.
Initializing the Genie Logger
The first step to using Genie Logger is to initialize the logger in your application. Here’s how you can do it:
const genieLogger = require('genie-logger'); const logger = genieLogger.createLogger({ level: 'info', transports: [ new genieLogger.transports.Console(), new genieLogger.transports.File({ filename: 'app.log' }) ] });
Logging Messages
With Genie Logger, you can log messages of various severity levels such as info, warn, and error. Below are some examples:
logger.info('This is an informational message'); logger.warn('This is a warning message'); logger.error('This is an error message');
Custom Logging Levels
In addition to the default levels, you can define custom logging levels to suit your application’s needs:
const customLevels = { levels: { debug: 0, info: 1, notice: 2, warning: 3, error: 4 }, colors: { debug: 'blue', info: 'green', notice: 'yellow', warning: 'orange', error: 'red' } }; const logger = genieLogger.createLogger({ levels: customLevels.levels, transports: [ new genieLogger.transports.Console({ level: 'debug' }) ] }); genieLogger.addColors(customLevels.colors);
Logging HTTP Requests
Genie Logger provides middleware to log HTTP requests in your application. Here’s an example using Express.js:
const express = require('express'); const app = express(); app.use(genieLogger.expressLogger(logger)); app.get('/', (req, res) => { res.send('Hello, world!'); }); app.listen(3000, () => { logger.info('Server is running on port 3000'); });
Handling Exceptions
Genie Logger can also handle uncaught exceptions and unhandled promise rejections, making your application more robust:
const logger = genieLogger.createLogger({ level: 'info', transports: [ new genieLogger.transports.Console(), new genieLogger.transports.File({ filename: 'exceptions.log' }) ], exceptionHandlers: [ new genieLogger.transports.File({ filename: 'exceptions.log' }), new genieLogger.transports.Console() ] }); process.on('unhandledRejection', (reason, promise) => { logger.error('Unhandled Rejection:', reason); });
App Example with Genie Logger
Let’s integrate all the introduced APIs into a simple Node.js application:
const express = require('express'); const genieLogger = require('genie-logger'); const logger = genieLogger.createLogger({ level: 'info', transports: [ new genieLogger.transports.Console(), new genieLogger.transports.File({ filename: 'app.log' }) ], exceptionHandlers: [ new genieLogger.transports.File({ filename: 'exceptions.log' }), new genieLogger.transports.Console() ] }); const app = express(); app.use(genieLogger.expressLogger(logger)); app.get('/', (req, res) => { res.send('Hello, world!'); }); app.listen(3000, () => { logger.info('Server is running on port 3000'); }); process.on('unhandledRejection', (reason, promise) => { logger.error('Unhandled Rejection:', reason); });
Hash: 95491273444f7d54f241d29063bbeefb306e4c3f81397b697b8a83d7c4de223a