Friday, September 20, 2024 12:14:20 AM
> settings

Customize


Authenticate

> logger.js
const ColorsJS = require('colors'),
    Moment = require("moment");

module.exports = class Logger {
    constructor(esmbot) {
        this.ESMBot = esmbot;
    }

    async info(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        let output = `${Moment().format('YYYY-MM-DD HH:mm:ss')} [INFO] `;
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            output += `<${info.file} ${info.method}> `;
        }
        if (this.ESMBot.util.isObject(message)) {
            output += JSON.stringify(message);
        } else {
            output += `${message}`;
        }

        console.log(output);

        // this.catchAll({
        //     description: this.ESMBot.util.isObject(message) ? JSON.stringify(message) : `${message}`,
        //     color: this.ESMBot.colors.BLUE,
        //     timestamp: Moment.utc()
        // }, info);
    }

    async success(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        let output = `${Moment().format('YYYY-MM-DD HH:mm:ss')} [SUCCESS] `;
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            output += `<${info.file} ${info.method}> `;
        }
        if (this.ESMBot.util.isObject(message)) {
            output += JSON.stringify(message);
        } else {
            output += `${message}`;
        }

        console.log(output.green);

        // this.catchAll({
        //     description: this.ESMBot.util.isObject(message) ? JSON.stringify(message) : `${message}`,
        //     color: this.ESMBot.colors.GREEN,
        //     timestamp: Moment.utc()
        // }, info);
    }

    async warn(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        let output = `${Moment().format('YYYY-MM-DD HH:mm:ss')} [WARN] `;
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            output += `<${info.file} ${info.method}> `;
        }
        if (this.ESMBot.util.isObject(message)) {
            output += JSON.stringify(message);
        } else {
            output += `${message}`;
        }

        console.log(output.yellow);

        // this.catchAll({
        //     description: this.ESMBot.util.isObject(message) ? JSON.stringify(message) : `${message}`,
        //     color: this.ESMBot.colors.YELLOW,
        //     timestamp: Moment.utc()
        // }, info);
    }

    async error(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        let output = `${Moment().format('YYYY-MM-DD HH:mm:ss')} [ERROR] `;
        let description = "";
        let log = "";
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            output += `<${info.file} ${info.method}> `;
        }
        if (this.ESMBot.util.isObject(message)) {
            if (message instanceof Error) {
                output += message.stack;
                description = `\`\`\`${message.stack}\`\`\``;
                log = message.stack;
            } else {
                output += JSON.stringify(message);
                description = `\`\`\`${JSON.stringify(message)}\`\`\``;
                log = JSON.stringify(message);
            }
        } else {
            output += `${message}`;
            description = `\`\`\`${message}\`\`\``;
            log = `${message}`;
        }

        console.log(output.red);
    }

    async debug(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        let output = `${Moment().format('YYYY-MM-DD HH:mm:ss')} [DEBUG] `;
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            output += `<${info.file} ${info.method}> `;
        }
        if (this.ESMBot.util.isObject(message)) {
            output += JSON.stringify(message);
        } else {
            output += `${message}`;
        }
        console.log(output.cyan);
        // this.catchAll({
        //     description: this.ESMBot.util.isObject(message) ? JSON.stringify(message) : `${message}`,
        //     color: this.ESMBot.colors.WHITE,
        //     timestamp: Moment.utc()
        // }, info);
    }

    async trace(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        let output = `${Moment().format('YYYY-MM-DD HH:mm:ss')} [TRACE] `;
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            output += `<${info.file} ${info.method}> `;
        }
        if (this.ESMBot.util.isObject(message)) {
            output += JSON.stringify(message);
        } else {
            output += `${message}`;
        }
        console.log(output.magenta);
        // this.catchAll({
        //     description: this.ESMBot.util.isObject(message) ? JSON.stringify(message) : `${message}`,
        //     color: this.ESMBot.colors.ORANGE,
        //     timestamp: Moment.utc()
        // }, info);
    }

    async api(originalMessage, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(originalMessage)) return;
        let message = originalMessage;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.API, message);
    }

    async catchAll(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(message)) return;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.CATCH_ALL, message);
    }

    async cleanup(originalMessage, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(originalMessage)) return;
        let message = originalMessage;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.CLEANUP, message);
    }

    async commands(originalMessage, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(originalMessage)) return;
        let message = originalMessage;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.COMMANDS, message);
    }

    async communities(originalMessage, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(originalMessage)) return;
        let message = originalMessage;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.COMMUNITIES, message);
    }

    async dev(originalMessage, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(originalMessage)) return;
        let message = originalMessage;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.LOGGING.DEVELOPMENT, message);
    }

    async errors(message, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(message)) return;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.RED,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.ERRORS, message);
    }

    async registrations(originalMessage, info = {
        file: __caller.file,
        method: __caller.method
    }) {
        if (this.ESMBot.util.isNull(this.ESMBot.client)) return;
        if (this.ESMBot.util.isEmpty(originalMessage)) return;
        let message = originalMessage;
        if (!this.ESMBot.util.isObject(message)) {
            message = {
                description: `${message}`,
                color: this.ESMBot.colors.BLUE,
                timestamp: Moment.utc()
            };
        }
        if (!this.ESMBot.util.isEmpty(info, ["file", "method"])) {
            message.title = `${info.file} ${info.method}`;
        }

        this.ESMBot.send(this.ESMBot.config.DEBUG ? this.ESMBot.config.LOGGING.DEVELOPMENT : this.ESMBot.config.LOGGING.REGISTRATIONS, message);
    }
};
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8