Introduction to Reliable Logger
The reliable-logger is a powerful logging library designed for developers who need consistent and reliable logging capabilities in their applications. Its robust set of APIs makes it one of the most versatile logging tools available.
Installation
To get started with reliable-logger, you can install it via npm:
npm install reliable-logger
Basic Usage
Here is a simple example to show basic usage:
const ReliableLogger = require('reliable-logger'); const logger = new ReliableLogger(); logger.log('info', 'This is an informational message'); logger.log('error', 'This is an error message');
Custom Log Levels
Reliable Logger allows you to define custom log levels:
logger.setLevels({ levels: { debug: 0, info: 1, warning: 2, error: 3, }, colors: { debug: 'blue', info: 'green', warning: 'yellow', error: 'red', } }); logger.log('debug', 'This is a debug message');
Log Formatting
You can customize the format of the logs:
logger.format = (level, message) => { return `${new Date().toISOString()} [${level.toUpperCase()}]: ${message}`; }; logger.log('info', 'This message has a custom format');
Asynchronous Logging
The library supports asynchronous logging:
logger.logAsync('info', 'This is an asynchronous log message').then(() => { console.log('Message logged asynchronously'); });
File Logging
Logs can be written to a file:
logger.setFile('app.log'); logger.log('info', 'This message will be logged to the file');
Application Example
Below is a complete example of a small application using various reliable-logger APIs.
const ReliableLogger = require('reliable-logger'); const express = require('express'); const app = express(); const logger = new ReliableLogger(); logger.setLevels({ levels: { debug: 0, info: 1, warning: 2, error: 3, }, colors: { debug: 'blue', info: 'green', warning: 'yellow', error: 'red', } }); logger.format = (level, message) => { return `${new Date().toISOString()} [${level.toUpperCase()}]: ${message}`; }; logger.setFile('app.log'); app.get('/', (req, res) => { logger.log('info', 'Home page accessed'); res.send('Hello, World!'); }); app.listen(3000, () => { logger.log('info', 'Server is running on port 3000'); });
This example sets up a simple Express server and logs various events using reliable-logger’s API.
Hash: 0ca1e907850c63a79432467aa5838496f29fe19d1417ba750bcf6cabdbae026e