client/Logger.js

"use strict";
// Logger.ts - Logger (noud02)
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const Eris = require("eris");
const moment = require("moment");
const clk = new chalk_1.constructor({ enabled: true });
/**
 * Logger
 *
 * @param {string} prefix Prefix to use
 * @param {boolean} debugMode Enable debug mode
 * @export
 * @class Logger
 */
class Logger {
    constructor(prefix, debugMode = false) {
        this.prefix = prefix;
        this.debugMode = debugMode;
    }
    /**
     * Normal log
     *
     * @param {...any[]} args
     * @returns {void}
     */
    log(...args) {
        return console.log(this.base("log", ...args));
    }
    /**
     * Logs as error
     *
     * @param {...any[]} args
     * @returns {void}
     */
    err(...args) {
        return console.error(this.base("err", ...args));
    }
    /**
     * Logs as debug (only if debug mode is enabled)
     *
     * @param {...any[]} args
     * @returns {void}
     */
    debug(...args) {
        if (this.debugMode) {
            return console.log(this.base("debug", ...args));
        }
    }
    /**
     * Logs as fail
     *
     * @param {...any[]} args
     * @returns {void}
     */
    fail(...args) {
        return console.error(this.base("fail", ...args));
    }
    /**
     * Logs as info
     *
     * @param {...any[]} args
     * @returns {void}
     */
    info(...args) {
        return console.info(this.base("info", ...args));
    }
    /**
     * Logs as ok
     *
     * @param {...any[]} args
     * @returns {void}
     */
    ok(...args) {
        return console.log(this.base("ok", ...args));
    }
    /**
     * Logs a message
     *
     * @param {Eris.Message} msg
     * @returns {void}
     */
    msg(msg) {
        const str = [];
        if (msg.channel instanceof Eris.GuildChannel) {
            str.push(clk.bold.magenta(msg.channel.guild.name));
            str.push("->");
            str.push(clk.bold.cyan(msg.channel.name));
            str.push("->");
        }
        str.push(clk.bold.green(`${msg.author.username}#${msg.author.discriminator}`));
        str.push("->");
        str.push(msg.cleanContent || clk.italic.dim("empty"));
        return console.log(this.base("msg", ...str));
    }
    /**
     * Get a colored label based on type
     *
     * @private
     * @param {string} type
     * @returns {string}
     */
    getLabel(type) {
        switch (type) {
            case "info":
                return clk.bgMagenta(" INF ");
            case "msg":
                return clk.bgCyan(" MSG ");
            case "ok":
                return clk.bgGreen(" OK! ");
            case "fail":
                return clk.bgRed(" FAI ");
            case "err":
                return clk.bgRed(" ERR ");
            case "debug":
                return clk.black.bgWhite(" DBG ");
            case "log":
            default:
                return clk.black.bgWhite(" LOG ");
        }
    }
    /**
     * Base log thing
     *
     * @private
     * @param {string} type
     * @param {...any[]} args
     * @returns {string}
     */
    base(type, ...args) {
        const date = clk.cyan(moment().format("L"));
        const time = clk.cyan(moment().format("LTS"));
        const m = (str) => clk.magenta.bold(str);
        const now = `${m("[")} ${date} ${m("@")} ${time} ${m("]")}`;
        const prefix = clk.yellow(this.prefix);
        const label = this.getLabel(type);
        return `${prefix} ${now} ${label} ${args.join(" ")}`;
    }
}
exports.Logger = Logger;