Package Exports
- @beherapratyush/logger
- @beherapratyush/logger/dist/smart-logger.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@beherapratyush/logger) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Smart Logger
A lightweight and customizable logger for both browser and Node.js environments. Supports log levels (debug, info, warn, error) and colored output.
Features
- Universal: Works in both browser and Node.js environments.
- Custom Log Levels: Set the minimum log level to control output.
- Colored Output: Logs are color-coded for better readability.
- Lightweight: No external dependencies.
Installation
Install the package via npm:
npm install @beherapratyush/loggerOr via yarn:
yarn add @beherapratyush/loggerUsage
Basic Usage
import SmartLogger from "@beherapratyush/logger";
// Create a logger instance
const logger = new SmartLogger({ logLevel: "info" });
// Log messages
logger.debug("This is a debug message"); // Won't log (logLevel is 'info')
logger.info("This is an info message"); // Will log
logger.warn("This is a warning message"); // Will log
logger.error("This is an error message"); // Will logSet Log Level Dynamically
You can change the log level at runtime:
logger.setLogLevel("debug"); // Now debug logs will be shown
logger.debug("This is a debug message"); // Will logSmart Logger Instance
new SmartLogger(options)
Creates a new logger instance.
- options (Object):
logLevel(String): Minimum log level to display. Default:'info'.- Possible values:
'debug','info','warn','error'.
Methods
logger.debug(message, ...args): Logs a debug message.logger.info(message, ...args): Logs an info message.logger.warn(message, ...args): Logs a warning message.logger.error(message, ...args): Logs an error message.logger.setLogLevel(level): Sets the minimum log level.
Examples
Browser Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logger Test</title>
<script src="https://unpkg.com/@beherapratyush/logger/dist/smart-logger.js"></script>
<script>
const logger = new SmartLogger({ logLevel: "debug" });
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warning message");
logger.error("This is an error message");
</script>
</head>
<body>
<h1>Check the console for logs!</h1>
</body>
</html>Node Example
const SmartLogger = require('@beherapratyush/logger');
const logger = new SmartLogger({ logLevel: 'warn' });
logger.debug('This is a debug message'); // Won't log
logger.info('This is an info message'); // Won't log
logger.warn('This is a warning message'); // Will log
logger.error('This is an error message'); // Will logBuilding the Package
If you want to build the package locally:
1. Clone the repository:
git clone https://github.com/pratyushbehera/smart-logger.git
cd smart-logger2. Install dependencies:
npm install3. Build the package:
npm run buildThis will generate a dist folder containing the bundled and minified files.
Contributing
Contributions are welcome! Follow these steps:
1. Fork the repository.
2. Create a new branch:
git checkout -b feature/your-feature3.Make your changes and commit them:
git commit -m "Add your feature"4.Push to the branch:
git push origin feature/your-feature5.Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
If you find this package useful, please consider giving it a ⭐️ on GitHub.
For issues or feature requests, please open an issue on the GitHub repository.
Enjoy logging! 🚀