@hack4impact/logger
Version:
The lightweight & lightning-fast Logger Utility used by Hack4Impact Projects
600 lines (573 loc) • 17.6 kB
JavaScript
module.exports =
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ 100:
/***/ ((__unused_webpack_module, exports) => {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.COLORS = void 0;
/**
*
* ANSI Escape Sequences
*
*/
exports.COLORS = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
};
/***/ }),
/***/ 976:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.NO_LOGS_PATH = exports.INVALID_TYPE = void 0;
var index_1 = __nccwpck_require__(144);
exports.INVALID_TYPE = new TypeError("Value for 'type' parameter must be one of " + index_1.LOG_TYPES);
exports.NO_LOGS_PATH = new Error("No 'logsPath' was entered when initializing this Logger instance");
/***/ }),
/***/ 144:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Output = exports.Logger = exports.LOG_TYPES = exports.CONSOLE_LEVELS = void 0;
// Externals
var assert_1 = __nccwpck_require__(357);
var fs_1 = __nccwpck_require__(747);
var writeFile = fs_1.promises.writeFile;
// Internals
var errors_1 = __nccwpck_require__(976);
var colors_1 = __nccwpck_require__(100);
/**
*
* Array of all possible console log levels
*
*/
exports.CONSOLE_LEVELS = ["log", "warn", "error"];
/**
*
* Array of all possible log types
*
*/
exports.LOG_TYPES = ["success", "info", "error", "warn"];
var Logger = /** @class */ (function () {
/**
*
* Creates a new Logger instance
*
* @param logsPath - The output file path
* @example
* ```javascript
* const logger = new Logger({ logsPath: __dirname + "/logs.json" });
* ```
*/
function Logger(options) {
this._logsPath = options === null || options === void 0 ? void 0 : options.logsPath;
this._logs = [];
}
Object.defineProperty(Logger.prototype, "logs", {
/**
*
* All logs that have been output by this Logger instance
*
* @example
* ```javascript
* const logs = logger.logs;
* ```
*/
get: function () {
return this._logs;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Logger.prototype, "logsPath", {
/**
*
* The output file path
*
* @example
* ```javascript
* const logsPath = logger.logsPath;
* ```
*/
get: function () {
return this._logsPath;
},
/**
*
* The output file path
*
* @example
* ```javascript
* logger.logsPath = __dirname + "/logs.json";
* ```
*/
set: function (logsPath) {
this._logsPath = logsPath;
},
enumerable: false,
configurable: true
});
Logger.prototype.log = function (message, logOptions) {
var type = (logOptions !== null && logOptions !== void 0 ? logOptions : {}).type;
var log = {
timestamp: Date.now(),
index: this.logs.length,
message: message,
type: type,
};
if (type) {
assert_1.strict.ok(exports.LOG_TYPES.includes(type), errors_1.INVALID_TYPE);
Logger[type](message);
}
else
Logger.log(message);
if (!(logOptions && logOptions.writeToFile === false)) {
var extra = (logOptions !== null && logOptions !== void 0 ? logOptions : {}).extra;
log = __assign(__assign({}, log), { extra: extra });
this.logs.push(log);
return this.writeLogs().then(function () { return log; });
}
this.logs.push(log);
return log;
};
Logger.prototype.success = function (message, logOptions) {
var options = __assign(__assign({}, logOptions), { type: "success" });
if (options.writeToFile === false)
return this.log(message, options);
return this.log(message, options); // Same as above, but different overload and return value
};
Logger.prototype.info = function (message, logOptions) {
var options = __assign(__assign({}, logOptions), { type: "info" });
if (options.writeToFile === false)
return this.log(message, options);
return this.log(message, options); // Same as above, but different overload and return value
};
Logger.prototype.warn = function (message, logOptions) {
var options = __assign(__assign({}, logOptions), { type: "warn" });
if (options.writeToFile === false)
return this.log(message, options);
return this.log(message, options); // Same as above, but different overload and return value
};
Logger.prototype.error = function (message, logOptions) {
var options = __assign(__assign({}, logOptions), { type: "error" });
if (options.writeToFile === false)
return this.log(message, options);
return this.log(message, options); // Same as above, but different overload and return value
};
/**
*
* Writes the logs stored in `this.logs` to the output file
*
* @example
* ```javascript
* await logger.writeLogs();
* ```
*/
Logger.prototype.writeLogs = function () {
if (!this.logsPath)
throw errors_1.NO_LOGS_PATH;
return writeFile(this.logsPath, JSON.stringify(this.logs), "utf-8");
};
/**
*
* Logs to the console
*
* @param message - The message to log
* @param optionalParams - Substitution strings
* @example
* ```javascript
* Logger.log("hi");
* ```
* @example
* ```javascript
* Logger.log("hi %s", "Bill");
* ```
*/
Logger.log = function (message) {
var optionalParams = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalParams[_i - 1] = arguments[_i];
}
console.log.apply(console, __spreadArrays([message], optionalParams));
};
/**
*
* Logs an empty line to the console
*
* @example
* ```javascript
* Logger.line();
* ```
*/
Logger.line = function () {
console.log();
};
/**
*
* Logs a colored message to the console
*
* @param color - The color to log in
* @param message - The message to log
* @param afterColored - The optional message after the colored message (on the same line)
* @param consoleLevel - The console level to use (log, warn, or error)
* @example
* ```javascript
* Logger.coloredLog("BgBlue", "hi");
* ```
* @example
* ```javascript
* Logger.coloredLog("FgYellow", "hi", "this string will not be colored");
* ```
* @example
* ```javascript
* Logger.coloredLog("FgRed", "error!!!", "not colored", "error");
* ```
*/
Logger.coloredLog = function (color, message, afterColored, consoleLevel) {
if (afterColored === void 0) { afterColored = ""; }
if (consoleLevel === void 0) { consoleLevel = "log"; }
console[consoleLevel]("" + this.COLORS[color] + message + this.COLORS.Reset + afterColored);
};
/**
*
* Logs a bold message
*
* @param message - The bold message to log
* @param afterColored - The optional message after the bold message (on the same line)
* @example
* ```javascript
* Logger.bold("BOLD!");
* ```
* @example
* ```javascript
* Logger.bold("BOLD!", "this part is not bold");
* ```
*/
Logger.bold = function (message, afterColored) {
if (afterColored === void 0) { afterColored = ""; }
return this.coloredLog("Bright", message, afterColored);
};
/**
*
* Logs a success message in green
*
* @param message - The success message to log
* @param afterColored - The optional message after the success message (on the same line)
* @example
* ```javascript
* Logger.success("SUCCESS!");
* ```
* @example
* ```javascript
* Logger.success("SUCCESS!", "this part is not green");
* ```
*/
Logger.success = function (message, afterColored) {
if (afterColored === void 0) { afterColored = ""; }
return this.coloredLog("FgGreen", message, afterColored);
};
/**
*
* Logs an info message in blue
*
* @param message - The info message to log
* @param afterColored - The optional message after the info message (on the same line)
* @example
* ```javascript
* Logger.info("information...");
* ```
* @example
* ```javascript
* Logger.info("information...", "this part is not blue");
* ```
*/
Logger.info = function (message, afterColored) {
if (afterColored === void 0) { afterColored = ""; }
return this.coloredLog("FgBlue", message, afterColored);
};
/**
*
* Logs a warning message in yellow
*
* @param message - The warning message to log
* @param afterColored - The optional message after the warning message (on the same line)
* @example
* ```javascript
* Logger.warn("WARNING!");
* ```
* @example
* ```javascript
* Logger.warn("WARNING!", "this part is not yellow");
* ```
*/
Logger.warn = function (message, afterColored) {
if (afterColored === void 0) { afterColored = ""; }
return this.coloredLog("FgYellow", message, afterColored, "warn");
};
/**
*
* Logs an error message in red
*
* @param message - The error message to log
* @param afterColored - The optional message after the error message (on the same line)
* @example
* ```javascript
* Logger.error("ERROR!");
* ```
* @example
* ```javascript
* Logger.error("ERROR!", "this part is not red");
* ```
*/
Logger.error = function (message, afterColored) {
if (afterColored === void 0) { afterColored = ""; }
return this.coloredLog("FgRed", message, afterColored, "error");
};
/**
*
* ANSI Escape Sequences
*
* @example
* ```javascript
* console.log(Logger.COLORS.Dim + "Dim log" + Logger.COLORS.Reset);
* ```
*/
Logger.COLORS = colors_1.COLORS;
return Logger;
}());
exports.Logger = Logger;
var output_1 = __nccwpck_require__(768);
Object.defineProperty(exports, "Output", ({ enumerable: true, get: function () { return output_1.Output; } }));
exports.default = Logger;
/***/ }),
/***/ 768:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Output = void 0;
var colors_1 = __nccwpck_require__(100);
/**
* Store and flush a series of outputs synchronously
*/
var Output = /** @class */ (function () {
function Output() {
/**
*
* All messages or nested outputs of this Output instance
*
*/
this._output = [];
}
Object.defineProperty(Output.prototype, "output", {
/**
*
* All messages or nested outputs of this Output instance
*
*/
get: function () {
return this._output;
},
/**
*
* All messages or nested outputs of this Output instance
*
*/
set: function (newOutput) {
this._output = newOutput;
},
enumerable: false,
configurable: true
});
/**
*
* Adds a message to the output array
*
* @param value - The message to add
* @example
* ```javascript
* new Output().log("hi");
* ```
*/
Output.prototype.log = function (value) {
this.output.push(value);
return value;
};
/**
*
* Adds a line break to the output array
*
* @example
* ```javascript
* new Output().line();
* ```
*/
Output.prototype.line = function () {
this.output.push("");
};
/**
*
* Adds a colored message to the output array
*
* @param color - The color to log in
* @param message - The message to add
* @param afterColored - The optional message after the colored message (on the same line)
* @returns The colored log message
* @example
* ```javascript
* new Output().coloredLog("BgBlue", "hi");
* ```
* @example
* ```javascript
* new Output().coloredLog("FgYellow", "hi", "this string will not be colored");
* ```
*/
Output.prototype.coloredLog = function (color, message, afterColored) {
if (afterColored === void 0) { afterColored = ""; }
var colored = "" + Output.COLORS[color] + message + Output.COLORS.Reset + afterColored;
this.output.push(colored);
return colored;
};
/**
*
* Adds a nested output instance to the output array
*
* @returns The nested output
* @example
* ```javascript
* const nested = new Output().nested();
* ```
*/
Output.prototype.nested = function () {
var nestedOutput = new Output();
this.output.push(nestedOutput);
return nestedOutput;
};
/**
*
* Logs all messages in the output array
*
* @returns The output array
* @example
* ```javascript
* new Output().flush();
* ```
*/
Output.prototype.flush = function () {
this.output.forEach(function (value) {
if (value instanceof Output)
value.flush();
else
console.log(value);
});
var flushed = __spreadArrays(this.output);
this.output = [];
return flushed;
};
/**
*
* ANSI Escape Sequences
*
* @example
* ```javascript
* const bold = Output.Colors.Bright;
* ```
*/
Output.COLORS = colors_1.COLORS;
return Output;
}());
exports.Output = Output;
exports.default = Output;
/***/ }),
/***/ 357:
/***/ ((module) => {
module.exports = require("assert");;
/***/ }),
/***/ 747:
/***/ ((module) => {
module.exports = require("fs");;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __nccwpck_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ var threw = true;
/******/ try {
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __nccwpck_require__);
/******/ threw = false;
/******/ } finally {
/******/ if(threw) delete __webpack_module_cache__[moduleId];
/******/ }
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __nccwpck_require__(144);
/******/ })()
;