UNPKG

@koreanpanda/inscriber

Version:

A Logger that can write logs, and print them, with full customization.

114 lines (113 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorClass = void 0; /**======================================================================== * ? ABOUT * @author : Cody Spratford * @email : koreanpanda345@gmail.com * @repo : https://github.com/koreanpanda345/Inscriber * @createdOn : 11/14/2020 * @description : This is the Error Class. This handles all the error type's * methods. * @since : 11/15/2020 *========================================================================**/ var chalk = require("chalk"); var moment = require("moment"); var FileSystem_1 = require("../FileSystem"); /**======================================================================== * ERROR CLASS * - TODO Add Javascript Error Support. * - TODO Add More support to other Error Types. *========================================================================**/ var ErrorClass = /** @class */ (function () { function ErrorClass(config, content, source) { if (config["write-logs"]) { if (config["one-file"]) { if (typeof config["log-paths"].info !== "string") throw new TypeError( "Type Provided for Info's Log Path is not a string. The type must be a string.", ); this.write( content instanceof Error ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof TypeError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof EvalError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof RangeError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof SyntaxError ? content.name + "\n " + content.message + "\n" + content.stack : content.toString(), source, config["log-paths"].info, config, ); } else { if (typeof config["log-paths"].error !== "string") throw new TypeError( "Type Provided for Error's Log Path is not a string. The type must be a string.", ); this.write( content instanceof Error ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof TypeError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof EvalError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof RangeError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof SyntaxError ? content.name + "\n " + content.message + "\n" + content.stack : content.toString(), source, config["log-paths"].error, config, ); } } this.print( content instanceof Error ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof TypeError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof EvalError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof RangeError ? content.name + "\n" + content.message + "\n" + content.stack : content instanceof SyntaxError ? content.name + "\n " + content.message + "\n" + content.stack : content.toString(), source, config, ); } ErrorClass.prototype.write = function (content, source, filepath, config) { var message = this.formatString(config, source, content); new FileSystem_1.FileSystem(config, moment().format("YYYY_MM_D") + ".log", message, "ERROR").write(); }; ErrorClass.prototype.print = function (content, source, config) { var message = this.formatString(config, source, content); console.log(this.formatColors(config, message)); }; ErrorClass.prototype.formatString = function (config, source, content) { var str = config["log-patterns"].error; str = str.replace("<time>", moment().format(config["time-pattern"])); str = str.replace("<source>", source); str = str.replace("<type>", "ERROR"); str = str.replace("<message>", content); return str; }; ErrorClass.prototype.formatColors = function (config, content) { var color = config["log-colors"].error; if (color.background.startsWith("#")) { if (color.text.startsWith("#")) return chalk.hex(color.text).bgKeyword(color.background)(content); else return chalk.keyword(color.text).bgHex(color.background)(content); } else { if (color.text.startsWith("#")) return chalk.hex(color.text).bgKeyword(color.background)(content); else return chalk.keyword(color.text).bgKeyword(color.background)(content); } }; return ErrorClass; })(); exports.ErrorClass = ErrorClass;