JSPM

@beherapratyush/logger

1.0.2
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 3
    • Score
      100M100P100Q27794F
    • License MIT

    A customizable logging utility for Node.js and browser environments.

    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

    License Version Downloads

    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/logger

    Or via yarn:

    yarn add @beherapratyush/logger

    Usage

    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 log

    Set 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 log

    Smart 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 log

    Building 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-logger

    2. Install dependencies:

    npm install

    3. Build the package:

    npm run build

    This 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-feature

    3.Make your changes and commit them:

    git commit -m "Add your feature"

    4.Push to the branch:

    git push origin feature/your-feature

    5.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! 🚀