client.m

Custom Logger Module

This module provides a set of functions for logging messages to the console with customizable styles, including support for different text and background colors in HEX, RGB, and HSL formats.

Installation

Ensure you have the chalk library installed in your project:

npm i chalk@4.1.0

src/functions/msg.js

const chalk = require("chalk");

module.exports = (client) => {
    client.m = {
        success: (msg, options = {}) => { 
            console.log(formatMessage(chalk.green, msg, options));
        },
        danger: (msg, options = {}) => { 
            console.log(formatMessage(chalk.red, msg, options));
        },
        warning: (msg, options = {}) => { 
            console.log(formatMessage(chalk.yellow, msg, options));
        },
        info: (msg, options = {}) => { 
            console.log(formatMessage(chalk.blue, msg, options));
        },
        custom: async (text, color, bg, options = {}) => {
            let styledText = chalk;

            //color
            if (color) styledText = applyColor(styledText, color);

            //background color
            if (bg) styledText = applyBgColor(styledText, bg);

            //text styles
            if (options.bold) styledText = styledText.bold;
            if (options.underline) styledText = styledText.underline;
            if (options.italic) styledText = styledText.italic;
            if (options.strikethrough) styledText = styledText.strikethrough;

            console.log(styledText(text));
        }
    };
};

//apply custom styles to messages
function formatMessage(colorFn, msg, options) {
    if (options.bold) msg = chalk.bold(msg);
    if (options.underline) msg = chalk.underline(msg);
    if (options.italic) msg = chalk.italic(msg);
    if (options.strikethrough) msg = chalk.strikethrough(msg);

    return colorFn(msg);
}

//apply color based on format
function applyColor(chalkInstance, color) {
    if (/^#([0-9A-F]{3}){1,2}$/i.test(color)) {
        return chalkInstance.hex(color);
    } else if (/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/i.test(color)) {
        const [r, g, b] = color.match(/\d+/g).map(Number);
        return chalkInstance.rgb(r, g, b);
    } else if (/^hsl\((\d{1,3}),\s*(\d{1,3})%,\s*(\d{1,3})%\)$/i.test(color)) {
        const [h, s, l] = color.match(/\d+/g).map(Number);
        return chalkInstance.hsl(h, s, l);
    } else {
        throw new Error("Invalid color format. Please use HEX, RGB, or HSL.");
    }
}

//apply background color based on format
function applyBgColor(chalkInstance, bg) {
    if (/^#([0-9A-F]{3}){1,2}$/i.test(bg)) {
        return chalkInstance.bgHex(bg);
    } else if (/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/i.test(bg)) {
        const [r, g, b] = bg.match(/\d+/g).map(Number);
        return chalkInstance.bgRgb(r, g, b);
    } else if (/^hsl\((\d{1,3}),\s*(\d{1,3})%,\s*(\d{1,3})%\)$/i.test(bg)) {
        const [h, s, l] = bg.match(/\d+/g).map(Number);
        return chalkInstance.bgHsl(h, s, l);
    } else {
        throw new Error("Invalid background color format. Please use HEX, RGB, or HSL.");
    }
}

Available Methods

The logger provides several methods for logging messages with different levels of importance, as well as a method for fully custom styles.

client.m.success(msg, options)

Logs a success message in green.

  • Parameters:

    • msg (string): The message to log.

    • options (object, optional): Additional styling options (bold, underline, italic, strikethrough).

  • Example:

client.m.success("Operation successful!", { bold: true, underline: true });

client.m.danger(msg, options)

Logs a danger or error message in red.

  • Parameters:

    • msg (string): The message to log.

    • options (object, optional): Additional styling options (bold, underline, italic, strikethrough).

  • Example:

client.m.danger("Error occurred!", { italic: true });

client.m.warning(msg, options)

Logs a warning message in yellow.

  • Parameters:

    • msg (string): The message to log.

    • options (object, optional): Additional styling options (bold, underline, italic, strikethrough).

  • Example:

client.m.warning("This is a warning!", { bold: true });

client.m.info(msg, options)

Logs an informational message in blue.

  • Parameters:

    • msg (string): The message to log.

    • options (object, optional): Additional styling options (bold, underline, italic, strikethrough).

  • Example:

client.m.info("Just some information.", { underline: true });

client.m.custom(text, color, bg, options)

Logs a message with custom text and background colors, supporting HEX, RGB, and HSL formats.

  • Parameters:

    • text (string): The message to log.

    • color (string): The text color in HEX (#RRGGBB or #RGB), RGB (rgb(255,255,255)), or HSL (hsl(360, 100%, 50%)).

    • bg (string): The background color in HEX, RGB, or HSL formats.

    • options (object, optional): Additional styling options (bold, underline, italic, strikethrough).

  • Example:

client.m.custom("Custom message with HEX color", "#ff5733", "#333", { bold: true, strikethrough: true });
client.m.custom("Custom message with RGB color", "rgb(255,0,0)", "rgb(0,0,0)", { italic: true });
client.m.custom("Custom message with HSL color", "hsl(120,100%,50%)", "hsl(240,100%,50%)", { underline: true });

Customization Options

All methods accept an options object for additional text styling:

  • bold: Makes the text bold.

  • underline: Underlines the text.

  • italic: Italicizes the text.

  • strikethrough: Strikes through the text.

Example

client.m.success("Operation successful!", { bold: true, underline: true });
client.m.danger("Error occurred!", { italic: true });
client.m.warning("This is a warning!", { bold: true });
client.m.info("Just some information.", { underline: true });

client.m.custom("Custom message with HEX color", "#ff5733", "#333", { bold: true, strikethrough: true });
client.m.custom("Custom message with RGB color", "rgb(255,0,0)", "rgb(0,0,0)", { italic: true });
client.m.custom("Custom message with HSL color", "hsl(120,100%,50%)", "hsl(240,100%,50%)", { underline: true });

Error Handling

  • The custom method will throw an error if an invalid color format is provided. Valid formats include HEX (#RRGGBB or #RGB), RGB (rgb(255, 255, 255)), and HSL (hsl(360, 100%, 50%)).

  • Example of Invalid Color Error:

client.m.custom("Invalid color format example", "invalid-color", "#000");
// Throws: Error: Invalid color format. Please use HEX, RGB, or HSL.

Last updated