@koreanpanda/inscriber
Version:
A Logger that can write logs, and print them, with full customization.
78 lines (77 loc) • 3.8 kB
JavaScript
;
/**========================================================================
* ? ABOUT
* @author : Cody Spratford
* @email : koreanpanda345@gmail.com
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the Info Class. This handles all the info type's
* methods.
* @since : 11/15/2020
*========================================================================**/
Object.defineProperty(exports, "__esModule", { value: true });
exports.InfoClass = void 0;
var chalk = require("chalk");
var moment = require("moment");
var FileSystem_1 = require("../FileSystem");
var InfoClass = /** @class */ (function () {
function InfoClass(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.toString(), source, config["log-paths"].info, config);
} else {
if (typeof config["log-paths"].info !== "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, config["log-paths"].info, config);
}
}
this.print(content.toString(), source, config);
}
/**==============================================
* @summary Writes to the warn logs to the log files.
* @param content - The warn log
* @param source - the source of the warn log.
* @param filepath - the file location for the log.
* @param config - The configuration of Inscriber.
*=============================================**/
InfoClass.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, "INFO").write();
};
/**==============================================
* @summary Prints the warn log to the console.
* @param content - the warn log.
* @param source - The source of the warn log.
* @param config - the configuration of Inscriber.
*=============================================**/
InfoClass.prototype.print = function (content, source, config) {
var message = this.formatString(config, source, content);
console.log(this.formatColors(config, message));
};
InfoClass.prototype.formatColors = function (config, content) {
var color = config["log-colors"].info;
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);
}
};
InfoClass.prototype.formatString = function (config, source, content) {
var str = config["log-patterns"].info;
str = str.replace("<time>", moment().format(config["time-pattern"]));
str = str.replace("<source>", source);
str = str.replace("<type>", "INFO");
str = str.replace("<message>", content);
return str;
};
return InfoClass;
})();
exports.InfoClass = InfoClass;