UNPKG

summit-logger

Version:

A logger/debug tool for Summit University Dev Students. Creates "pretty-print" log messages, pleasant to the eye and easy to digest. (Summit University All Rights Reserved)

128 lines (83 loc) 3.39 kB
# summit-logger A lightweight and easy-to-use logging/debugging tool specifically designed for Summit University development students. `summit-logger` provides "pretty-printed" log messages in the browser console, making debugging a more pleasant and efficient experience. ## Features **Namespaces:** Organize your logs using namespaces (e.g., `SummitUniversity:Main`, `SummitUniversity:Auth`). **Extensible Namespaces:** Create sub-namespaces for even finer-grained control (e.g., `SummitUniversity:Main:UI`, `SummitUniversity:Auth:Login`). **Filtering:** Enable or disable logs based on namespaces using `localStorage`. Use wildcards (`*`) for broad filtering. **Pretty Printing:** Logs are formatted with timestamps and distinct colors, making them easy to read and understand. Objects and arrays are nicely formatted using `JSON.stringify` with indentation. **Data Type Handling:** Handles various data types, just like `console.log` (objects, arrays, strings, numbers, booleans, null, undefined, symbols). **Easy to Use:** Simple API with a familiar `console.log`-like interface. **Persistent Debugging:** Uses `localStorage` to remember which logs are enabled across page reloads. **MIT License:** Freely use and modify the package. ## Installation ```bash yarn add summit-logger ``` ## Usage ### 1. Import and Create a Logger ```javascript import _summitLogger from "summit-logger"; export const logger = _summitLogger("APP"); ``` ### 2. Extend Loggers for Components Each component can create its own logger by extending the main `APP` logger: ```javascript import { logger } from "./App"; const loginLogger = logger.extend("Login"); const spaLogger = logger.extend("SPA"); ``` ### 3. Log Messages ```javascript logger.log("Application started"); loginLogger.log("User clicked login", { username: "Alice" }); spaLogger.log("User navigated to dashboard"); ``` #### Example Output in Console ```text APP: Application started APP:Login: User clicked login { username: "Alice" } APP:SPA: User navigated to dashboard ``` ### 4. Enable or Disable Logging Use `localStorage` to control logging dynamically: #### Enable Specific Logs ```javascript localStorage.setItem("debug", "APP,APP:Login"); ``` #### Enable All Logs ```javascript localStorage.setItem("debug", "*"); ``` #### Disable Logging ```javascript localStorage.removeItem("debug"); ``` ## API Reference ### `log(message, ...args)` Logs a message to the console with optional additional data. ```javascript logger.log("Hello, world!"); logger.log("User details", { id: 1, name: "John Doe" }); ``` ### `extend(subspace)` Creates a sub-logger under the current namespace. ```javascript const authLogger = logger.extend("Auth"); authLogger.log("Authentication started"); ``` ### `enable()` Enables logging for this logger's namespace. ```javascript logger.enable(); ``` ### `disable()` Disables logging for this logger's namespace. ```javascript logger.disable(); ``` ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## Contributing Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change. ## Author Developed and maintained by **Summit University**.