custom-log-js

1.1.0 • Public • Published

Smart Logger

License Version Downloads

A lightweight and customizable logger for both browser and Node.js environments. Supports log levels (trace,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 custom-log-js

Or via yarn:

yarn add custom-log-js

Usage

Basic Usage

const Logger = require("custom-log-js");

// Create a logger instance
const logger = new Logger({ logLevel: "info" });

// Log messages
logger.trace("This is a tarce message"); // Won't log (logLevel is 'info')
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 Logger(options)

Creates a new logger instance.

  • options (Object):
    • logLevel (String): Minimum log level to display. Default: 'info'.
    • Possible values: 'trace', 'debug', 'info', 'warn', 'error'.

Methods

  • logger.trace(message, ...args): Logs a trace message.
  • 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/custom-log-js/dist/logger.js"></script>
    <script>
      const logger = new Logger({ logLevel: "debug" });

      logger.trace("This is a trace message");
      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 Logger = require('custom-log-js');
const logger = new Logger({ logLevel: 'warn' });

logger.trace('This is a trace message'); // Won't log
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! 🚀

Package Sidebar

Install

npm i custom-log-js

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

18.1 kB

Total Files

5

Last publish

Collaborators

  • beherapratyush