UNPKG

@koreanpanda/inscriber

Version:

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

129 lines (128 loc) 5.73 kB
"use strict"; /**======================================================================== * ? ABOUT * @author : Cody Spratford * @email : koreanpanda345@gmail.com * @repo : https://github.com/koreanpanda345/Inscriber * @createdOn : 11/21/2020 * @description : This is the log method which will allow the users to make their own log methods. * @since : 11/21/2020 *========================================================================**/ Object.defineProperty(exports, "__esModule", { value: true }); exports.LogMethod = void 0; var chalk = require("chalk"); var moment = require("moment"); var FileSystem_1 = require("../FileSystem"); var LogMethod = /** @class */ (function () { function LogMethod(config, content, source, type) { var _a, _b, _c; if ( !config["custom-logs"].find(function (x) { return x.name == type; }) ) throw new Error("Couldn't find that log."); 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.toString(), source, config["log-paths"].info, type, config); } else { if ( typeof ((_a = config["custom-logs"].find(function (x) { return x.name == type; })) === null || _a === void 0 ? void 0 : _a.path) !== "string" ) throw new TypeError( "Type Provided for Error's Log Path is not a string. The type must be a string.", ); this.write( content.toString(), source, (_c = (_b = config["custom-logs"].find(function (x) { return x.name == type; })) === null || _b === void 0 ? void 0 : _b.path) !== null && _c !== void 0 ? _c : "", type, config, ); } } this.print(content.toString(), source, type, config); } LogMethod.prototype.write = function (content, source, filepath, type, config) { var _a; if ( config["custom-logs"].find(function (x) { return x.name == type; }) == undefined ) throw new Error("I couldn't find a custom log type with the name of " + type); var log = config["custom-logs"].find(function (x) { return x.name == type; }); if ((log === null || log === void 0 ? void 0 : log.path) != undefined && typeof log.path !== "string") throw new TypeError( "The custom log path of " + log.name + " is not a string. Found " + typeof log.path + " instead.", ); var message = this.formatString( source, content, (_a = log === null || log === void 0 ? void 0 : log.pattern) !== null && _a !== void 0 ? _a : "", type, config["time-pattern"], ); new FileSystem_1.FileSystem(config, moment().format("YYYY_MM_D") + ".log", message, "LOG", type).write(); }; LogMethod.prototype.print = function (content, source, type, config) { if ( config["custom-logs"].find(function (x) { return x.name == type; }) == undefined ) throw new Error("I couldn't find a custom log type with the name of " + type); var log = config["custom-logs"].find(function (x) { return x.name == type; }); if ((log === null || log === void 0 ? void 0 : log.pattern) != undefined && typeof log.pattern != "string") throw new TypeError( "The custom log patter of " + log.name + " is not a string. Found " + typeof log.pattern + " instead.", ); var pattern = log === null || log === void 0 ? void 0 : log.pattern; var str = this.formatString( source, content, pattern !== null && pattern !== void 0 ? pattern : "", type, config["time-pattern"], ); if (log != undefined) console.log(this.formatColors(log, str)); }; LogMethod.prototype.formatColors = function (config, content) { var color = config.color; 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); } }; LogMethod.prototype.formatString = function (source, content, pattern, type, timePattern) { pattern = pattern.replace("<time>", moment().format(timePattern)); pattern = pattern.replace("<source>", source); pattern = pattern.replace("<type>", type.toUpperCase()); pattern = pattern.replace("<message>", content); return pattern; }; return LogMethod; })(); exports.LogMethod = LogMethod;