|
| 1 | +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { |
| 2 | + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { |
| 3 | + if (ar || !(i in from)) { |
| 4 | + if (!ar) ar = Array.prototype.slice.call(from, 0, i); |
| 5 | + ar[i] = from[i]; |
| 6 | + } |
| 7 | + } |
| 8 | + return to.concat(ar || Array.prototype.slice.call(from)); |
| 9 | +}; |
| 10 | +var Logger = /** @class */ (function () { |
| 11 | + function Logger() { |
| 12 | + this.logLevels = ["log", "info", "warn", "error"]; |
| 13 | + this.logLevel = "log"; |
| 14 | + } |
| 15 | + Logger.prototype.setLevel = function (logLevel) { |
| 16 | + // Set the log level |
| 17 | + this.logLevel = logLevel; |
| 18 | + }; |
| 19 | + Object.defineProperty(Logger, "instance", { |
| 20 | + get: function () { |
| 21 | + if (Logger._instance === undefined) { |
| 22 | + Logger._instance = new Logger(); |
| 23 | + } |
| 24 | + return Logger._instance; |
| 25 | + }, |
| 26 | + enumerable: false, |
| 27 | + configurable: true |
| 28 | + }); |
| 29 | + // Helper method to get the current timestamp |
| 30 | + Logger.prototype.getTimestamp = function () { |
| 31 | + var now = new Date(); |
| 32 | + return now.toISOString(); |
| 33 | + }; |
| 34 | + // Method to check if a message should be logged based on log level |
| 35 | + Logger.prototype.shouldLog = function (level) { |
| 36 | + var currentLevelIndex = this.logLevels.indexOf(this.logLevel); |
| 37 | + var messageLevelIndex = this.logLevels.indexOf(level); |
| 38 | + return messageLevelIndex >= currentLevelIndex; |
| 39 | + }; |
| 40 | + // Main log method that accepts multiple arguments |
| 41 | + Logger.prototype.log = function () { |
| 42 | + var args = []; |
| 43 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 44 | + args[_i] = arguments[_i]; |
| 45 | + } |
| 46 | + if (this.shouldLog("log")) { |
| 47 | + console.log.apply(console, __spreadArray(["%cLOG:", |
| 48 | + "color: white; background-color: #405d27", "[".concat(this.getTimestamp(), "]")], args, false)); |
| 49 | + } |
| 50 | + }; |
| 51 | + // Method to log errors |
| 52 | + Logger.prototype.error = function () { |
| 53 | + var args = []; |
| 54 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 55 | + args[_i] = arguments[_i]; |
| 56 | + } |
| 57 | + if (this.shouldLog("error")) { |
| 58 | + console.error.apply(console, __spreadArray(["%cERROR:", |
| 59 | + "color: white; background-color: #c94c4c", "[".concat(this.getTimestamp(), "]")], args, false)); |
| 60 | + } |
| 61 | + }; |
| 62 | + // Method to log warnings |
| 63 | + Logger.prototype.warn = function () { |
| 64 | + var args = []; |
| 65 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 66 | + args[_i] = arguments[_i]; |
| 67 | + } |
| 68 | + if (this.shouldLog("warn")) { |
| 69 | + console.warn.apply(console, __spreadArray(["%cWARNING:", |
| 70 | + "color: black; background-color: #feb236", "[".concat(this.getTimestamp(), "]")], args, false)); |
| 71 | + } |
| 72 | + }; |
| 73 | + // Method to log info |
| 74 | + Logger.prototype.info = function () { |
| 75 | + var args = []; |
| 76 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 77 | + args[_i] = arguments[_i]; |
| 78 | + } |
| 79 | + if (this.shouldLog("info")) { |
| 80 | + console.info.apply(console, __spreadArray(["%cINFO:", |
| 81 | + "color: white; background-color: #4040a1", "[".concat(this.getTimestamp(), "]")], args, false)); |
| 82 | + } |
| 83 | + }; |
| 84 | + return Logger; |
| 85 | +}()); |
| 86 | +// Example usage of the Logger class |
| 87 | +Logger.instance.log("Test value", 5); |
| 88 | +// Set the log level to "info" |
| 89 | +Logger.instance.setLevel("info"); |
| 90 | +// This will log a "log" message (will not appear because the log level is "info") |
| 91 | +Logger.instance.log("This is a log message that won't be displayed."); |
| 92 | +// This will log an "info" message |
| 93 | +Logger.instance.info("This is an info message."); |
| 94 | +// This will log a "warn" message |
| 95 | +Logger.instance.warn("This is a warning message."); |
| 96 | +// This will log an "error" message |
| 97 | +Logger.instance.error("This is an error message."); |
| 98 | +// Change log level to "warn" |
| 99 | +Logger.instance.setLevel("warn"); |
| 100 | +// This will log a "warn" message (will be displayed) |
| 101 | +Logger.instance.warn("Another warning message."); |
| 102 | +// This will log an "error" message (will be displayed) |
| 103 | +Logger.instance.error("Another error message."); |
| 104 | +// This will not log an "info" message (will not be displayed) |
| 105 | +Logger.instance.info("This info message will not be displayed because the log level is set to warn."); |
0 commit comments