JS Logs Formatter Documentation
website·Indexed 30 days ago
https://js-logs-formatter-docs-v3.vercel.app/docs/introductionA lightweight logging utility designed to format server logs for improved readability in tools like React Native and Node.js. Features the println function for customized log outputs with support for helper text and color formatting.
What's inside JS Logs Formatter
- JS Logs Formatter is a lightweight logging utility designed to improve the readability of server logs. It provides structured log outputs, making it particularly useful for debugging in environments such as React Native, Node.js, Next.js server components, and Single Page Applications (SPAs).
Customize log output with helper text and colors using println
The
printlnfunction fromjs-logs-formatterallows you to enhance console logs by adding a descriptive helper string and styling the output with colors. Every call toprintlnautomatically includes the function name and line number where the log was triggered.Available styling options include standard colors (e.g.,
green,red) and background colors (e.g.,bgRed).import { println } from "js-logs-formatter"; // Example 1: Logging user data with a green label println({ helper: "User Data", data: user, color: "green", }); // Example 2: Logging an error with a red label try { loginUser(); } catch (error) { println({ helper: "loginUser error", data: error.response.data.message, color: "red", }); } // Example 3: Logging an error with a red background try { loginUser(); } catch (error) { println({ helper: "loginUser error", data: error.response.data.message, color: "bgRed", }); }Install JS Logs Formatter
Install thejs-logs-formatterpackage using npm, yarn, or pnpm to begin formatting your server logs for better readability.npm i js-logs-formatterLog formatted data using the println function
Theprintlnfunction fromjs-logs-formatterallows you to log data to the console in a highly readable, formatted structure compared to standard console output. It accepts a configuration object where you can specify the data to be logged and whether to show the function origin.import { println } from "js-logs-formatter"; const user = { name: "Jane Doe", age: 28, active: false, email: "janedoe@example.com", phone: "+1234567890", address: { street: "123 Main St", city: "Anytown", state: "CA", zipCode: "12345", }, preferences: { newsletter: true, notifications: { email: true, sms: false, }, }, createdAt: "2022-01-15T14:30:00Z", lastLogin: "2023-09-28T10:00:00Z", roles: ["user", "admin"], points: 1500, }; println({ data: user, showFunctionOrigin: false, });Set up js-logs-formatter for local development
To contribute to the js-logs-formatter project, follow these steps to set up the development environment locally: clone the repository, navigate into the project directory, and install the necessary dependencies.# Clone the repository git clone https://github.com/Elue-dev/js-logs-formatter.git # Navigate to the project directory cd js-logs-formatter # Install dependencies (using pnpm) pnpm installSubmit a Pull Request to js-logs-formatter
After implementing changes and ensuring they adhere to coding standards and are covered by tests, follow these steps to submit a Pull Request (PR):
- Push your local branch to your forked repository.
- Navigate to the original repository on GitHub, go to the 'Pull Requests' tab, and select 'New Pull Request'.
- Select your branch and provide a clear description of the changes.
- Request reviews from maintainers for feedback and merging.
git push origin your-branch-nameCustomize log output using the println function
The
printlnfunction accepts a configuration object to customize log outputs. Available properties include:data: The content to be logged (any type, e.g., object, array, string). Required.helper: A string describing the context of the log message to help identify the data.color: Specifies the color of the logged data. Defaults toreset.showFunctionOrigin: A boolean that determines if the function name and line number whereprintlnwas called are displayed. Defaults totrue.
Configure log colors in the println function
Use the
colorprop in theprintlnfunction to change the appearance of log outputs. The following color options are supported:Foreground Colors:
- Standard:
reset,red,green,yellow,blue,magenta,cyan,white,black - Bright:
brightRed,brightGreen,brightYellow,brightBlue,brightMagenta,brightCyan,brightWhite
Background Colors:
- Standard:
bgRed,bgGreen,bgYellow,bgBlue,bgMagenta,bgCyan,bgWhite,bgBlack - Bright:
bgBrightRed,bgBrightGreen,bgBrightYellow,bgBrightBlue,bgBrightMagenta,bgBrightCyan,bgBrightWhite
- Standard:
Configure println options for log formatting
The
printlnfunction accepts a configuration object with the following properties:helper(string): A descriptive label that appears before the data to identify the log's purpose.data(any): The actual content or object to be printed to the console.color(string): The color style for the log. Supported values includegreen,red, and background variants likebgRed.
Configure println options
Theprintlnfunction takes an options object to control the output. Thedataproperty is required to specify the object or value to log. TheshowFunctionOriginproperty can be set tofalseto hide the origin of the function call in the console output.println({ data: yourDataVariable, showFunctionOrigin: false, });