|
| 1 | +# js-dlogger (logger for js frameworks) |
| 2 | + |
| 3 | + |
| 4 | +## **📌 Description** |
| 5 | + |
| 6 | + js-dlogger is a lightweight, configurable logger for JavaScript, React.js, and React Native applications. It provides structured logging with different levels (info, debug, dLog, Warn, Error) and supports environment-based logging. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## **Installation** |
| 13 | + |
| 14 | +Install the package via npm: |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install js-dlogger |
| 18 | + |
| 19 | +``` |
| 20 | + |
| 21 | +or |
| 22 | +```bash |
| 23 | +yarn add js-dlogger |
| 24 | + |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage : |
| 28 | + |
| 29 | +## **✅ Basic Usage** |
| 30 | +Here’s an example to get you started: |
| 31 | + |
| 32 | +```javascript |
| 33 | +import Logger from "js-dlogger"; |
| 34 | + |
| 35 | +Logger.info("App", "INIT", "Application started"); |
| 36 | +Logger.debug("App", "DEBUG", "This is a debug message"); |
| 37 | +Logger.dLog("App", "DATA", { key: "value" }); |
| 38 | +Logger.warn("App", "WARNING", "This is a warning message"); |
| 39 | +Logger.error("App", "ERROR", "An error occurred"); |
| 40 | +``` |
| 41 | +## **📌 Logger with File Context** |
| 42 | +Here’s an example to get you started: |
| 43 | +```javascript |
| 44 | +import {LoggerWithFile} from 'js-dlogger/loggerWrapper'; |
| 45 | + |
| 46 | +const Logger = LoggerWithFile("Login.jsx"); |
| 47 | +Logger.info("INIT", "Application started"); |
| 48 | +Logger.debug("DEBUG", "This is a debug message"); |
| 49 | +Logger.dLog("DATA", { key: "value" }); |
| 50 | +Logger.warn("WARNING", "This is a warning message"); |
| 51 | +Logger.error("ERROR", "An error occurred"); |
| 52 | +``` |
| 53 | + |
| 54 | +## **🌎 Environment-Based Logging** |
| 55 | +The logger dynamically determines the environment: |
| 56 | +- **React**: If globalThis.__APP_ENV__ is set, it will be used. |
| 57 | +- **React Native**: If logger.config.js is available, it loads the environment from there. |
| 58 | +- **Any js-framework** : Otherwise, it defaults to development. |
| 59 | + |
| 60 | +**Example logger.config.js:** |
| 61 | +```javascript |
| 62 | +/** |
| 63 | + * Config file for {logger} |
| 64 | + * @return Object : ENV value |
| 65 | + * @alias : development | production |
| 66 | + * @development ~ for development environment |
| 67 | + * @production ~ release & disable debug logs |
| 68 | + * @author structlooper |
| 69 | + * */ |
| 70 | +export default { |
| 71 | + ENV: 'production', |
| 72 | +}; |
| 73 | +``` |
| 74 | +## **🛠 Best Practices** |
| 75 | +**1️⃣ Ensure the Config File Exists (If Required)** |
| 76 | + |
| 77 | +If your project needs logger.config.js, make sure to create it: |
| 78 | +```javascript |
| 79 | +export default { |
| 80 | + ENV: 'production', |
| 81 | +}; |
| 82 | +``` |
| 83 | +**2️⃣ Use File-Based Logging for Easier Debugging** |
| 84 | + |
| 85 | +Instead of passing taq manually, use LoggerWithFile for consistency. |
| 86 | +```javascript |
| 87 | +const Logger = LoggerWithFile("Login.jsx") |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## **⚡️ Troubleshooting** |
| 93 | + |
| 94 | +| Issue | Solution | |
| 95 | +|------------------------------------|-------------------------------------------------------------------------| |
| 96 | +| Metro bundler error | Ensure `logger.config.js` exists or use the `try-catch` approach. | |
| 97 | +| Environment is always development | Check if `globalThis.__APP_ENV__` or `logger.config.js` is correctly set. | |
| 98 | +--- |
| 99 | +## **📜 License** |
| 100 | + |
| 101 | +This project is licensed under the MIT License. See the [LICENSE](https://opensource.org/licenses/MIT) file for details. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## **Contributing** |
| 106 | + |
| 107 | +Contributions, issues, and feature requests are welcome! |
| 108 | +Feel free to check the [issues page](https://github.com/structlooper/js-dlogger/issues). |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## **📧 Contact** |
| 113 | + |
| 114 | +For support, feedback, or contributions, reach out via GitHub: [structlooper](https://github.com/structlooper). |
| 115 | + |
0 commit comments