@mahdi.golzar/advancelogger v1.0.0
AdvancedLogger
AdvancedLogger is a customizable logging utility written in JavaScript. It provides advanced logging capabilities with support for filtering log levels, dynamic message formatting, and easy log level adjustments.
Features
Log Levels: Support for multiple log levels (DEBUG, INFO, WARN, ERROR) to control which messages are displayed. Dynamic Formatting: Ability to set a custom formatter for log messages. Flexible Usage: Methods for different log levels for easy and structured logging. Installation No installation is required as this is a pure JavaScript class. You can simply include the class in your project.
Usage
Basic Setup
- Create an instance of AdvancedLogger:
const logger = new AdvancedLogger();- Set the log level: Set the current log level to control which messages are shown. For example, setting to INFO will hide DEBUG messages.
logger.setLevel('INFO'); // Set log level to INFO- Log messages: Use the provided methods to log messages at different levels:
logger.debug('This is a debug message'); // Won't be shown if level is INFO
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');- Custom Formatter: By default, log messages are formatted as LEVEL message. You can set a custom formatter:
logger.setFormatter((level, message) => `${new Date().toISOString()} - ${level}: ${message}`);API
constructor(): Initializes the logger with default settings. setLevel(level: string): Sets the minimum log level to display. Valid levels are DEBUG, INFO, WARN, ERROR. setFormatter(formatter: function): Sets a custom formatter function for log messages. log(level: string, message: string): Logs a message at the specified level if it meets the current level setting. debug(message: string): Logs a debug message. info(message: string): Logs an info message. warn(message: string): Logs a warning message. error(message: string): Logs an error message.
Example
Here’s a complete example of how to use the AdvancedLogger:
// Create a logger instance
const logger = new AdvancedLogger();
// Set the log level
logger.setLevel('INFO');
// Set a custom message format
logger.setFormatter((level, message) => `${new Date().toISOString()} - ${level}: ${message}`);
// Log messages
logger.debug('This is a debug message'); // Will not be shown
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');2 years ago