sonic-logger
v0.0.1-beta-008
Published
A powerful, extensible TypeScript logging library for Node.js
Maintainers
Readme
Super Logger
A powerful, extensible TypeScript logging library for Node.js. It provides fast, non-blocking asynchronous logging with support for multiple log levels, colorized output (for development), and customizable log formatting. Perfect for production-level applications where performance matters.
Table of Contents
Installation
You can easily install Super Logger using npm or yarn.
Using npm:
npm install super-loggerUsing yarn:
yarn add super-loggerUsage
Basic Usage
After installing the package, import and create a logger instance in your application.
import { SuperLogger } from 'super-logger';
// Create a new logger instance
const logger = new SuperLogger();
// Log messages with different levels
logger.info('This is an info log');
logger.error('This is an error log');
logger.warn('This is a warning log');
logger.debug('This is a debug log');
logger.trace('This is a trace log');Log Levels
Super Logger supports the following log levels:
info– Informational messageserror– Errors and exceptionswarn– Warningsdebug– Debugging informationtrace– Fine-grained debugging
Each log level is colorized based on its severity (in development mode), making it easy to distinguish between different types of logs.
logger.error('This is an error log'); // Red
logger.warn('This is a warning log'); // Yellow
logger.info('This is an info log'); // Green
logger.debug('This is a debug log'); // Cyan
logger.trace('This is a trace log'); // GrayCustom Log Formatting
You can customize the log format (e.g., including timestamp, log level, and metadata).
import { SuperLogger } from 'super-logger';
const logger = new SuperLogger({
colorize: true, // Enable colorization for development
format: 'json', // Format logs as JSON (for structured logging)
});
logger.info('This is an info log with a custom format');Features
- Fast, Async Logging: Logs are processed asynchronously to avoid blocking the main thread.
- Colorized Output: Provides colorized logs in development to easily distinguish between log levels.
- Customizable Log Format: Customize the log format to include timestamps, log levels, and metadata.
- File-Based Logging: Easily log to files, rotating logs by size or date to avoid memory overload.
- Error Handling: Gracefully handles uncaught exceptions and logs errors without crashing the application.
API Reference
SuperLogger
The core class of the package.
Constructor
new SuperLogger(options: SuperLoggerOptions)Parameters:
options: (Optional) Configuration options to customize the logger.
Methods
info(message: string): void: Logs an info level message.error(message: string): void: Logs an error level message.warn(message: string): void: Logs a warn level message.debug(message: string): void: Logs a debug level message.trace(message: string): void: Logs a trace level message.
Log Level Methods
Each log level method outputs the message to the console, with different formatting based on the severity of the log level.
info(message: string): Use this for informational messages.error(message: string): Use this for error messages.warn(message: string): Use this for warnings.debug(message: string): Use this for debug information.trace(message: string): Use this for fine-grained logs (usually only for debugging).
Options
SuperLoggerOptions allows you to configure the logger with various options.
interface SuperLoggerOptions {
colorize: boolean; // Enable or disable colorized output (default: true in development)
format: string; // Define the log format: 'default', 'json', or 'custom'
filePath: string; // Specify the file path for log file output (optional)
}Configuration
File Logging
You can configure Super Logger to log directly to a file, rotating logs based on size or time.
const logger = new SuperLogger({
colorize: false, // Disable colorization in production
filePath: '/logs/app.log', // Specify file path for logging
});Colorization
In development, logs are colorized by default to provide clear distinctions between log levels. In production, colorization is disabled to avoid unnecessary output overhead.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Feel free to contribute! Open an issue or submit a pull request for improvements, bug fixes, or new features.
Contact:
- Author: Aniruddh
- Email: [email protected]
- GitHub: https://github.com/aniruddh-214
