UNPKG

loglow

Version:

A flexible logging utility for Node.js, offering customizable color, style, and timestamp options for enhanced console output.

157 lines (156 loc) 4.95 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { LogColor: () => LogColor, LogStyle: () => LogStyle, Loglow: () => Loglow }); module.exports = __toCommonJS(src_exports); var LogColor = /* @__PURE__ */ ((LogColor2) => { LogColor2["Red"] = "\x1B[31m"; LogColor2["Green"] = "\x1B[32m"; LogColor2["Yellow"] = "\x1B[33m"; LogColor2["Blue"] = "\x1B[34m"; LogColor2["Magenta"] = "\x1B[35m"; LogColor2["Cyan"] = "\x1B[36m"; LogColor2["White"] = "\x1B[37m"; return LogColor2; })(LogColor || {}); var LogStyle = /* @__PURE__ */ ((LogStyle2) => { LogStyle2["Reset"] = "\x1B[0m"; LogStyle2["Bold"] = "\x1B[1m"; LogStyle2["Dim"] = "\x1B[2m"; LogStyle2["Italic"] = "\x1B[3m"; LogStyle2["Underscore"] = "\x1B[4m"; LogStyle2["Blink"] = "\x1B[5m"; LogStyle2["Reverse"] = "\x1B[7m"; return LogStyle2; })(LogStyle || {}); var Loglow = class { constructor() { this.data = []; } /** * Adds a message log entry with optional styling. * @param {...unknown[]} messages - One or more messages or data to log. * @returns {this} The instance of Loglow for chaining. */ add(...messages) { this.data.push({ type: "message" /* Message */, data: messages }); return this; } /** * Adds a timestamp log entry with an optional color or style. * @param {LogColor | LogStyle} [color=LogStyle.Reset] - The color or style of the timestamp. * @returns {this} The instance of Loglow for chaining. */ addTimestamp(color = "\x1B[0m" /* Reset */) { this.data.push({ type: "timestamp" /* Timestamp */, color, data: (/* @__PURE__ */ new Date()).toISOString() }); return this; } /** * Adds a newline log entry. * @returns {this} The instance of Loglow for chaining. */ addNewline() { this.data.push({ type: "newline" /* Newline */ }); return this; } /** * Checks if a given value is a valid LogColor or LogStyle. * @private * @param {string} value - The value to check. * @returns {boolean} True if the value is a valid LogColor or LogStyle, false otherwise. */ isLogColorOrStyle(value) { return Object.values(__spreadValues(__spreadValues({}, LogColor), LogStyle)).includes( value ); } /** * Prints the log entries to the console with appropriate formatting. */ print() { let output = ""; this.data.forEach((item) => { switch (item.type) { case "newline" /* Newline */: output += "\n"; break; case "timestamp" /* Timestamp */: output += `${item.color}${item.data}${"\x1B[0m" /* Reset */}`; break; case "message" /* Message */: let accMessage = ""; let count = 0; if (typeof item.data === "string") { output += `${item.data}`; break; } item.data.forEach((data) => { if (typeof data === "string" && this.isLogColorOrStyle(data)) { accMessage += `${data}`; } else { if (count > 0) { accMessage += " "; } accMessage += `${data}`; count++; } }); output += `${accMessage}${"\x1B[0m" /* Reset */}`; if (item.data.length > 0) { output += " "; } break; } }); console.log(output); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { LogColor, LogStyle, Loglow }); //# sourceMappingURL=index.js.map