diff --git a/package-lock.json b/package-lock.json index ac8da80..30ba182 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lunacrew/logger", - "version": "1.1.0", + "version": "2.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lunacrew/logger", - "version": "1.1.0", + "version": "2.0.0", "license": "MIT", "devDependencies": { "@types/jest": "29.5.11", @@ -1836,12 +1836,13 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, + "license": "MIT", "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -2522,10 +2523,11 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, + "license": "MIT", "dependencies": { "to-regex-range": "^5.0.1" }, @@ -2884,6 +2886,7 @@ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true, + "license": "MIT", "engines": { "node": ">=0.12.0" } @@ -4526,6 +4529,7 @@ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dev": true, + "license": "MIT", "dependencies": { "is-number": "^7.0.0" }, diff --git a/package.json b/package.json index 5a4d7ce..8824486 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lunacrew/logger", - "version": "1.1.0", + "version": "2.0.0", "description": "Node.js logger with dynamic log levels", "main": "dist/src/index.js", "types": "src/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 820a0e0..fd0986d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,23 +11,23 @@ import LogManager from './log/LogManager' * @method custom - The custom message to log. */ export default class Log { - static readonly d = (message: string, tag: string = '') => { + static readonly d = (message: string, tag?: string) => { return LogManager.debug(message, tag) } - static readonly i = (message: string, tag: string = '') => { + static readonly i = (message: string, tag?: string) => { return LogManager.info(message, tag) } - static readonly w = (message: string, tag: string = '') => { + static readonly w = (message: string, tag?: string) => { return LogManager.warn(message, tag) } - static readonly e = (message: string, tag: string = '') => { + static readonly e = (message: string, tag?: string) => { return LogManager.error(message, tag) } - static readonly v = (message: string, tag: string = '') => { + static readonly v = (message: string, tag?: string) => { return LogManager.verbose(message, tag) } diff --git a/src/log/LogManager.ts b/src/log/LogManager.ts index 201c9c4..d167593 100644 --- a/src/log/LogManager.ts +++ b/src/log/LogManager.ts @@ -2,10 +2,9 @@ import { Color } from '../types/Color' import formatTimestamp from '../helper/timestamp' import EnumColor from '../helper/EnumColor' -/** - * The `LogManager` class provides static methods for logging messages with different log levels. - * @constructor - */ +const timestamp = formatTimestamp(new Date()) +const { cyan, base, green, blue, yellow, red } = EnumColor + export default class LogManager { message: string @@ -13,114 +12,76 @@ export default class LogManager { this.message = message } - /** - * Logs a debug message with an optional tag. - * - * @param {string} message - The debug message to log. - * @param {string} tag - An optional tag to include in the log message. - */ - static readonly debug = (message: string, tag: string = ''): void => { - const timestamp = formatTimestamp(new Date()) - const { cyan, base, green, whiteOnGreen } = EnumColor - - if (tag === '') { - message = `${cyan}[ ${timestamp} ] ${whiteOnGreen} D ${base} => ${message}\n` - } else { - message = `${cyan}[ ${timestamp} ] ${green}:: ${tag} :: ${whiteOnGreen} D ${base} => ${message}\n` + static readonly debug = (message: string, tag?: string): void => { + if (!tag) tag = 'debug' + const log = `${cyan}[ ${timestamp} ] ${green}:: ${tag} :: ${base}=> ${message}\n` + const details = { + level: 'debug', + message: message, + tag: tag, + timestamp: timestamp, } - console.log(message) + + console.log(log, 'details:', details) } - /** - * Logs an informational message. - * - * @param {string} message - The message to log. - * @param {string} tag - An optional tag for the log message. - */ - static readonly info = (message: string, tag: string = ''): void => { - const timestamp = formatTimestamp(new Date()) - const { cyan, base, blue, whiteOnBlue } = EnumColor - - if (tag === '') { - message = `${cyan}[ ${timestamp} ] ${whiteOnBlue} I ${base} => ${message}\n` - } else { - message = `${cyan}[ ${timestamp} ] ${blue}:: ${tag} :: ${whiteOnBlue} I ${base} => ${message}\n` + static readonly info = (message: string, tag?: string): void => { + if (!tag) tag = 'info' + const log = `${cyan}[ ${timestamp} ] ${blue}:: ${tag} :: ${base}=> ${message}\n` + const details = { + level: 'info', + message: message, + tag: tag, + timestamp: timestamp, } - console.log(message) + + console.log(log, 'details:', details) } - /** - * Logs a warning message with an optional tag. - * - * @param {string} message - The warning message to log. - * @param {string} tag - An optional tag to include in the log message. - */ - static readonly warn = (message: string, tag: string = ''): void => { - const timestamp = formatTimestamp(new Date()) - const { cyan, base, yellow, whiteOnYellow } = EnumColor - - if (tag === '') { - message = `${cyan}[ ${timestamp} ] ${whiteOnYellow} W ${base} => ${message}\n` - } else { - message = `${cyan}[ ${timestamp} ] ${yellow}:: ${tag} :: ${whiteOnYellow} W ${base} => ${message}\n` + static readonly warn = (message: string, tag?: string): void => { + if (!tag) tag = 'warn' + const log = `${cyan}[ ${timestamp} ] ${yellow}:: ${tag} :: ${base}=> ${message}\n` + const details = { + level: 'warn', + message: message, + tag: tag, + timestamp: timestamp, } - console.log(message) + + console.log(log, 'details:', details) } - /** - * Logs an error message with an optional tag. - * @param {string} message - The error message to log. - * @param {string} tag - An optional tag to provide additional context for the error. - */ - static readonly error = (message: string, tag: string = ''): void => { - const timestamp = formatTimestamp(new Date()) - const { cyan, base, red, whiteOnRed } = EnumColor - - if (tag === '') { - message = `${cyan}[ ${timestamp} ] ${whiteOnRed} E ${base} => ${message}\n` - } else { - message = `${cyan}[ ${timestamp} ] ${red}:: ${tag} :: ${whiteOnRed} E ${base} => ${message}\n` + static readonly error = (message: string, tag?: string): void => { + if (!tag) tag = 'error' + const log = `${cyan}[ ${timestamp} ] ${red}:: ${tag} :: ${base}=> ${message}\n` + const details = { + level: 'error', + message: message, + tag: tag, + timestamp: timestamp, } - console.log(message) - throw new Error(message) + + console.log(log, 'details:', details) } - /** - * Logs a verbose message with an optional tag. - * @param {string} message - The message to be logged. - * @param {string} tag - An optional tag to provide additional context. - */ - static readonly verbose = (message: string, tag: string = ''): void => { - const timestamp = formatTimestamp(new Date()) - const { cyan, base, blackOnWhite } = EnumColor - - if (tag === '') { - message = `${cyan}[ ${timestamp} ] ${blackOnWhite} V ${base} => ${message}\n` - } else { - message = `${cyan}[ ${timestamp} ] ${base}:: ${tag} :: ${blackOnWhite} V ${base} => ${message}\n` + static readonly verbose = (message: string, tag?: string): void => { + if (!tag) tag = 'verbose' + const log = `${cyan}[ ${timestamp} ] ${base}:: ${tag} :: ${base}=> ${message}\n` + const details = { + level: 'verbose', + message: message, + tag: tag, + timestamp: timestamp, } - console.log(message) - } + console.log(log, 'details:', details) + } - /** - * Logs a custom message with optional formatting options. - * - * @param message - The message to log. - * @param options - The formatting options for the log message. - * @param options.tag - The tag to display before the message. - * @param options.tagColor - The color of the tag. - * @param options.tagIcon - The icon to display with the tag. - * @param options.iconColor - The color of the tag icon. - * @param options.messageColor - The color of the log message. - */ - static readonly custom = (message: string, options?: {tag?: string, tagColor?: Color, tagIcon?: string, iconColor?: Color, messageColor?: Color}): void => { + static readonly custom = (message: string, options?: { tag?: string, tagColor?: Color, tagIcon?: string, iconColor?: Color, messageColor?: Color }): void => { options = options || {} - const timestamp = formatTimestamp(new Date()) - const { cyan, base } = EnumColor - + options.tag = !options.tag ? '' : ` :: ${options.tag} :: ` - options.tagColor = !options.tagColor? 'base' : options.tagColor + options.tagColor = !options.tagColor ? 'base' : options.tagColor options.tagIcon = !options.tagIcon ? '' : ` ${options.tagIcon} ` options.iconColor = !options.iconColor ? 'base' : options.iconColor options.messageColor = !options.messageColor ? 'base' : options.messageColor @@ -129,7 +90,14 @@ export default class LogManager { const messageColorValue = EnumColor[options.messageColor] const iconColorValue = EnumColor[options.iconColor] - message = `${cyan}[ ${timestamp} ]${tagColorValue}${options.tag}${iconColorValue}${options.tagIcon}${base}${messageColorValue} => ${message}${base}\n` - console.log(message) + const log = `${cyan}[ ${timestamp} ]${tagColorValue}${options.tag}${iconColorValue}${options.tagIcon}${base}${messageColorValue} => ${message}${base}\n` + const details = { + level: 'custom', + message: message, + tag: options.tag, + timestamp: timestamp, + } + + console.log(log, 'details:', details) } } \ No newline at end of file diff --git a/test/index.spec.ts b/test/index.spec.ts index 4a59249..f5a8575 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -27,7 +27,7 @@ describe('Log', () => { Log.d(message) - expect(logManagerSpy).toHaveBeenCalledWith(message, '') + expect(logManagerSpy).toHaveBeenCalledWith(message, undefined) }) }) @@ -51,7 +51,7 @@ describe('Log', () => { Log.i(message) - expect(logManagerSpy).toHaveBeenCalledWith(message, '') + expect(logManagerSpy).toHaveBeenCalledWith(message, undefined) }) }) @@ -74,7 +74,7 @@ describe('Log', () => { Log.w(message) - expect(logManagerSpy).toHaveBeenCalledWith(message, '') + expect(logManagerSpy).toHaveBeenCalledWith(message, undefined) }) }) @@ -86,21 +86,18 @@ describe('Log', () => { it('should call LogManager.error when calling Log.e with tag', () => { const tag = 'Test' - const message = new Error('Error message') + const message = 'Error message' - const log = () => { - Log.e(`${message}`, tag) - } - expect(log).toThrow(Error) + Log.e(`${message}`, tag) + expect(logManagerSpy).toHaveBeenCalledWith(message, tag) }) it('should call LogManager.error when calling Log.e without tag', () => { - const message = new Error('Error message') + const message = 'Error message' + + Log.e(`${message}`) - const log = () => { - Log.e(`${message}`) - } - expect(log).toThrow(Error) + expect(logManagerSpy).toHaveBeenCalledWith(message, undefined) }) }) @@ -124,7 +121,7 @@ describe('Log', () => { Log.v(message) - expect(logManagerSpy).toHaveBeenCalledWith(message, '') + expect(logManagerSpy).toHaveBeenCalledWith(message, undefined) }) }) diff --git a/test/log/LogManager.spec.ts b/test/log/LogManager.spec.ts index 3b48f51..0eb753b 100644 --- a/test/log/LogManager.spec.ts +++ b/test/log/LogManager.spec.ts @@ -65,21 +65,17 @@ describe('LogManager', () => { const error = new Error('Error message') const tag = 'TestTag' - const log = () => { - LogManager.error(`${error}`, tag) - } + LogManager.error(`${error}`, tag) - expect(log).toThrow(Error) + expect(LogManager.error).toBeDefined() }) it('should log an error message without tag', () => { const error = new Error('Error message') - const log = () => { - LogManager.error(`${error}`) - } + LogManager.error(`${error}`) - expect(log).toThrow(Error) + expect(LogManager.error).toBeDefined() }) })