basant307/AI_Governance_Project
048
1var __defProp = Object.defineProperty;2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;3var __getOwnPropNames = Object.getOwnPropertyNames;4var __hasOwnProp = Object.prototype.hasOwnProperty;5var __export = (target, all) => {6 for (var name in all)7 __defProp(target, name, { get: all[name], enumerable: true });8};9var __copyProps = (to, from, except, desc) => {10 if (from && typeof from === "object" || typeof from === "function") {11 for (let key of __getOwnPropNames(from))12 if (!__hasOwnProp.call(to, key) && key !== except)13 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });14 }15 return to;16};17var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);18 19// src/index.ts20var src_exports = {};21__export(src_exports, {22 Logger: () => Logger23});24module.exports = __toCommonJS(src_exports);25var import_is_node_process = require("is-node-process");26var import_outvariant = require("outvariant");27 28// src/colors.ts29var colors_exports = {};30__export(colors_exports, {31 blue: () => blue,32 gray: () => gray,33 green: () => green,34 red: () => red,35 yellow: () => yellow36});37function yellow(text) {38 return `\x1B[33m${text}\x1B[0m`;39}40function blue(text) {41 return `\x1B[34m${text}\x1B[0m`;42}43function gray(text) {44 return `\x1B[90m${text}\x1B[0m`;45}46function red(text) {47 return `\x1B[31m${text}\x1B[0m`;48}49function green(text) {50 return `\x1B[32m${text}\x1B[0m`;51}52 53// src/index.ts54var IS_NODE = (0, import_is_node_process.isNodeProcess)();55var Logger = class {56 constructor(name) {57 this.name = name;58 this.prefix = `[${this.name}]`;59 const LOGGER_NAME = getVariable("DEBUG");60 const LOGGER_LEVEL = getVariable("LOG_LEVEL");61 const isLoggingEnabled = LOGGER_NAME === "1" || LOGGER_NAME === "true" || typeof LOGGER_NAME !== "undefined" && this.name.startsWith(LOGGER_NAME);62 if (isLoggingEnabled) {63 this.debug = isDefinedAndNotEquals(LOGGER_LEVEL, "debug") ? noop : this.debug;64 this.info = isDefinedAndNotEquals(LOGGER_LEVEL, "info") ? noop : this.info;65 this.success = isDefinedAndNotEquals(LOGGER_LEVEL, "success") ? noop : this.success;66 this.warning = isDefinedAndNotEquals(LOGGER_LEVEL, "warning") ? noop : this.warning;67 this.error = isDefinedAndNotEquals(LOGGER_LEVEL, "error") ? noop : this.error;68 } else {69 this.info = noop;70 this.success = noop;71 this.warning = noop;72 this.error = noop;73 this.only = noop;74 }75 }76 prefix;77 extend(domain) {78 return new Logger(`${this.name}:${domain}`);79 }80 /**81 * Print a debug message.82 * @example83 * logger.debug('no duplicates found, creating a document...')84 */85 debug(message, ...positionals) {86 this.logEntry({87 level: "debug",88 message: gray(message),89 positionals,90 prefix: this.prefix,91 colors: {92 prefix: "gray"93 }94 });95 }96 /**97 * Print an info message.98 * @example99 * logger.info('start parsing...')100 */101 info(message, ...positionals) {102 this.logEntry({103 level: "info",104 message,105 positionals,106 prefix: this.prefix,107 colors: {108 prefix: "blue"109 }110 });111 const performance2 = new PerformanceEntry();112 return (message2, ...positionals2) => {113 performance2.measure();114 this.logEntry({115 level: "info",116 message: `${message2} ${gray(`${performance2.deltaTime}ms`)}`,117 positionals: positionals2,118 prefix: this.prefix,119 colors: {120 prefix: "blue"121 }122 });123 };124 }125 /**126 * Print a success message.127 * @example128 * logger.success('successfully created document')129 */130 success(message, ...positionals) {131 this.logEntry({132 level: "info",133 message,134 positionals,135 prefix: `\u2714 ${this.prefix}`,136 colors: {137 timestamp: "green",138 prefix: "green"139 }140 });141 }142 /**143 * Print a warning.144 * @example145 * logger.warning('found legacy document format')146 */147 warning(message, ...positionals) {148 this.logEntry({149 level: "warning",150 message,151 positionals,152 prefix: `\u26A0 ${this.prefix}`,153 colors: {154 timestamp: "yellow",155 prefix: "yellow"156 }157 });158 }159 /**160 * Print an error message.161 * @example162 * logger.error('something went wrong')163 */164 error(message, ...positionals) {165 this.logEntry({166 level: "error",167 message,168 positionals,169 prefix: `\u2716 ${this.prefix}`,170 colors: {171 timestamp: "red",172 prefix: "red"173 }174 });175 }176 /**177 * Execute the given callback only when the logging is enabled.178 * This is skipped in its entirety and has no runtime cost otherwise.179 * This executes regardless of the log level.180 * @example181 * logger.only(() => {182 * logger.info('additional info')183 * })184 */185 only(callback) {186 callback();187 }188 createEntry(level, message) {189 return {190 timestamp: /* @__PURE__ */ new Date(),191 level,192 message193 };194 }195 logEntry(args) {196 const {197 level,198 message,199 prefix,200 colors: customColors,201 positionals = []202 } = args;203 const entry = this.createEntry(level, message);204 const timestampColor = customColors?.timestamp || "gray";205 const prefixColor = customColors?.prefix || "gray";206 const colorize = {207 timestamp: colors_exports[timestampColor],208 prefix: colors_exports[prefixColor]209 };210 const write = this.getWriter(level);211 write(212 [colorize.timestamp(this.formatTimestamp(entry.timestamp))].concat(prefix != null ? colorize.prefix(prefix) : []).concat(serializeInput(message)).join(" "),213 ...positionals.map(serializeInput)214 );215 }216 formatTimestamp(timestamp) {217 return `${timestamp.toLocaleTimeString(218 "en-GB"219 )}:${timestamp.getMilliseconds()}`;220 }221 getWriter(level) {222 switch (level) {223 case "debug":224 case "success":225 case "info": {226 return log;227 }228 case "warning": {229 return warn;230 }231 case "error": {232 return error;233 }234 }235 }236};237var PerformanceEntry = class {238 startTime;239 endTime;240 deltaTime;241 constructor() {242 this.startTime = performance.now();243 }244 measure() {245 this.endTime = performance.now();246 const deltaTime = this.endTime - this.startTime;247 this.deltaTime = deltaTime.toFixed(2);248 }249};250var noop = () => void 0;251function log(message, ...positionals) {252 if (IS_NODE) {253 process.stdout.write((0, import_outvariant.format)(message, ...positionals) + "\n");254 return;255 }256 console.log(message, ...positionals);257}258function warn(message, ...positionals) {259 if (IS_NODE) {260 process.stderr.write((0, import_outvariant.format)(message, ...positionals) + "\n");261 return;262 }263 console.warn(message, ...positionals);264}265function error(message, ...positionals) {266 if (IS_NODE) {267 process.stderr.write((0, import_outvariant.format)(message, ...positionals) + "\n");268 return;269 }270 console.error(message, ...positionals);271}272function getVariable(variableName) {273 if (IS_NODE) {274 return process.env[variableName];275 }276 return globalThis[variableName]?.toString();277}278function isDefinedAndNotEquals(value, expected) {279 return value !== void 0 && value !== expected;280}281function serializeInput(message) {282 if (typeof message === "undefined") {283 return "undefined";284 }285 if (message === null) {286 return "null";287 }288 if (typeof message === "string") {289 return message;290 }291 if (typeof message === "object") {292 return JSON.stringify(message);293 }294 return message.toString();295}296 