loglow
Version:
A flexible logging utility for Node.js, offering customizable color, style, and timestamp options for enhanced console output.
1 lines • 5.76 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Enumeration for log colors with ANSI escape codes for terminal coloring.\n */\nexport enum LogColor {\n Red = \"\\x1b[31m\",\n Green = \"\\x1b[32m\",\n Yellow = \"\\x1b[33m\",\n Blue = \"\\x1b[34m\",\n Magenta = \"\\x1b[35m\",\n Cyan = \"\\x1b[36m\",\n White = \"\\x1b[37m\",\n}\n\n/**\n * Enumeration for log styles with ANSI escape codes to style terminal output.\n */\nexport enum LogStyle {\n Reset = \"\\x1b[0m\",\n Bold = \"\\x1b[1m\",\n Dim = \"\\x1b[2m\",\n Italic = \"\\x1b[3m\",\n Underscore = \"\\x1b[4m\",\n Blink = \"\\x1b[5m\",\n Reverse = \"\\x1b[7m\",\n}\n\n/**\n * Enumeration for identifying the type of log entry.\n */\nenum DataType {\n Newline = \"newline\",\n Timestamp = \"timestamp\",\n Message = \"message\",\n}\n\n/**\n * Type definition for a log entry.\n * @property {DataType} type - The type of log entry.\n * @property {string | unknown[]} [data] - The content of the log entry, could be a message or other data types.\n * @property {LogColor | LogStyle} [color] - Optional color or style for the log entry.\n */\ntype LogEntry = {\n type: DataType;\n data?: string | unknown[];\n color?: LogColor | LogStyle;\n};\n\n/**\n * Class to create and manage a logger with support for colors, styles, and timestamps.\n */\nexport class Loglow {\n private data: LogEntry[] = [];\n\n /**\n * Adds a message log entry with optional styling.\n * @param {...unknown[]} messages - One or more messages or data to log.\n * @returns {this} The instance of Loglow for chaining.\n */\n public add(...messages: unknown[]): this {\n this.data.push({\n type: DataType.Message,\n data: messages,\n });\n return this;\n }\n\n /**\n * Adds a timestamp log entry with an optional color or style.\n * @param {LogColor | LogStyle} [color=LogStyle.Reset] - The color or style of the timestamp.\n * @returns {this} The instance of Loglow for chaining.\n */\n public addTimestamp(color: LogColor | LogStyle = LogStyle.Reset): this {\n this.data.push({\n type: DataType.Timestamp,\n color: color,\n data: new Date().toISOString(),\n });\n return this;\n }\n\n /**\n * Adds a newline log entry.\n * @returns {this} The instance of Loglow for chaining.\n */\n public addNewline(): this {\n this.data.push({ type: DataType.Newline });\n return this;\n }\n\n /**\n * Checks if a given value is a valid LogColor or LogStyle.\n * @private\n * @param {string} value - The value to check.\n * @returns {boolean} True if the value is a valid LogColor or LogStyle, false otherwise.\n */\n private isLogColorOrStyle(value: string): boolean {\n return Object.values({ ...LogColor, ...LogStyle }).includes(\n value as LogColor | LogStyle,\n );\n }\n\n /**\n * Prints the log entries to the console with appropriate formatting.\n */\n public print(): void {\n let output = \"\";\n this.data.forEach((item) => {\n switch (item.type) {\n case DataType.Newline:\n output += \"\\n\";\n break;\n case DataType.Timestamp:\n output += `${item.color}${item.data}${LogStyle.Reset}`;\n break;\n case DataType.Message:\n let accMessage = \"\";\n let count = 0;\n\n if (typeof item.data === \"string\") {\n output += `${item.data}`;\n break;\n }\n\n item.data!.forEach((data) => {\n if (typeof data === \"string\" && this.isLogColorOrStyle(data)) {\n accMessage += `${data}`;\n } else {\n if (count > 0) {\n accMessage += \" \";\n }\n accMessage += `${data}`;\n count++;\n }\n });\n\n output += `${accMessage}${LogStyle.Reset}`;\n\n if (item.data!.length > 0) {\n output += \" \";\n }\n\n break;\n }\n });\n console.log(output);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAGO,IAAK,WAAL,kBAAKA,cAAL;AACL,EAAAA,UAAA,SAAM;AACN,EAAAA,UAAA,WAAQ;AACR,EAAAA,UAAA,YAAS;AACT,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,aAAU;AACV,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,WAAQ;AAPE,SAAAA;AAAA,GAAA;AAaL,IAAK,WAAL,kBAAKC,cAAL;AACL,EAAAA,UAAA,WAAQ;AACR,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,SAAM;AACN,EAAAA,UAAA,YAAS;AACT,EAAAA,UAAA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