A lightweight and customizable logger for both browser and Node.js environments. Supports log levels (trace
,debug
, info
, warn
, error
) and colored output.
- 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.
Install the package via npm:
npm install custom-log-js
Or via yarn:
yarn add custom-log-js
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
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
Creates a new logger instance.
-
options (Object):
-
logLevel
(String): Minimum log level to display. Default:'info'
. - Possible values:
'trace'
,'debug'
,'info'
,'warn'
,'error'
.
-
-
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.
<!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>
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
If you want to build the package locally:
git clone https://github.com/pratyushbehera/smart-logger.git
cd smart-logger
npm install
npm run build
This will generate a dist
folder containing the bundled and minified files.
Contributions are welcome! Follow these steps:
git checkout -b feature/your-feature
git commit -m "Add your feature"
git push origin feature/your-feature
This project is licensed under the MIT License. See the LICENSE file for details.
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! 🚀