@ad-execute-manager/logger
v2.0.5
Published
A flexible logging utility for JavaScript applications with multiple log levels and customizable prefixes.
Maintainers
Readme
@ad-execute-manager/logger
A flexible logging utility for JavaScript applications with multiple log levels and customizable prefixes.
Installation
npm install @ad-execute-manager/loggerFeatures
- Multiple Log Levels: Support for error, warn, info, log, and debug levels
- Customizable Prefix: Add a prefix to identify the source of logs
- Log Level Control: Set minimum log level to filter out less important messages
- Enable/Disable: Toggle logging on and off as needed
- TypeScript Support: Includes TypeScript type definitions
Usage
Basic Usage
import { Logger } from '@ad-execute-manager/logger';
// Create a logger instance
const logger = new Logger({
prefix: 'MyApp',
level: 'info',
enabled: true
});
// Log messages at different levels
logger.error('Something went wrong');
logger.warn('This is a warning');
logger.info('Application started');
logger.log('Regular message');
logger.debug('Debug information');Log Level Control
import { Logger } from '@ad-execute-manager/logger';
// Create logger with different levels
const productionLogger = new Logger({
prefix: 'App',
level: 'warn' // Only show warn and error messages
});
const developmentLogger = new Logger({
prefix: 'App',
level: 'debug' // Show all messages
});
// Change level dynamically
productionLogger.setLevel('info');Enable/Disable Logging
import { Logger } from '@ad-execute-manager/logger';
const logger = new Logger({ prefix: 'MyApp' });
// Disable logging
logger.disable();
logger.info('This won\'t be logged');
// Enable logging
logger.enable();
logger.info('This will be logged');
// Check if logging is enabled
if (logger.isEnabled()) {
logger.log('Logging is enabled');
}Real-world Examples
1. Application Logging
import { Logger } from '@ad-execute-manager/logger';
// Create logger based on environment
const logger = new Logger({
prefix: 'MyApp',
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug'
});
// Log application startup
logger.info('Application started');
// Log errors
function handleError(error) {
logger.error('Error occurred:', error);
// Error handling logic
}
// Log debug information
function processData(data) {
logger.debug('Processing data:', data);
// Processing logic
logger.info('Data processed successfully');
}2. Module-specific Logging
import { Logger } from '@ad-execute-manager/logger';
// Create logger for different modules
const authLogger = new Logger({ prefix: 'AuthModule' });
const apiLogger = new Logger({ prefix: 'APIModule' });
const dbLogger = new Logger({ prefix: 'DBModule' });
// Use in different modules
function login(user) {
authLogger.info('User login attempt:', user.username);
// Login logic
}
function fetchData() {
apiLogger.debug('Fetching data from API');
// API call
apiLogger.info('Data fetched successfully');
}
function saveData(data) {
dbLogger.debug('Saving data to database');
// Database operation
dbLogger.info('Data saved successfully');
}API
Constructor
new Logger(options)- options (Object): Configuration options
- prefix (String): Log prefix, defaults to 'Logger'
- level (String): Log level, options: 'error', 'warn', 'info', 'log', 'debug', defaults to 'log'
- enabled (Boolean): Whether logging is enabled, defaults to true
Methods
- enable(): Enable logging
- disable(): Disable logging
- isEnabled(): Return whether logging is enabled
- setLevel(level): Set log level
- level (String): Log level
- error(message, ...args): Output error log
- warn(message, ...args): Output warning log
- info(message, ...args): Output info log
- log(message, ...args): Output regular log
- debug(message, ...args): Output debug log
Log Levels
| Level | Description | Methods | |---------|------------------------------|--------------------| | error | Error messages | logger.error() | | warn | Warning messages | logger.warn() | | info | Information messages | logger.info() | | log | Regular messages | logger.log() | | debug | Debug information | logger.debug() |
The log level is hierarchical, meaning if you set the level to 'info', it will show info, warn, and error messages, but not log or debug messages.
License
MIT
