Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
release/2.0.0 - add log details (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtrindade authored Aug 19, 2024
1 parent cc709f0 commit 43d28fc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 134 deletions.
22 changes: 13 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
162 changes: 65 additions & 97 deletions src/log/LogManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,86 @@ 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

constructor(message: string) {
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
Expand All @@ -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)
}
}
25 changes: 11 additions & 14 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Log', () => {

Log.d(message)

expect(logManagerSpy).toHaveBeenCalledWith(message, '')
expect(logManagerSpy).toHaveBeenCalledWith(message, undefined)
})
})

Expand All @@ -51,7 +51,7 @@ describe('Log', () => {

Log.i(message)

expect(logManagerSpy).toHaveBeenCalledWith(message, '')
expect(logManagerSpy).toHaveBeenCalledWith(message, undefined)
})
})

Expand All @@ -74,7 +74,7 @@ describe('Log', () => {

Log.w(message)

expect(logManagerSpy).toHaveBeenCalledWith(message, '')
expect(logManagerSpy).toHaveBeenCalledWith(message, undefined)
})
})

Expand All @@ -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)
})
})

Expand All @@ -124,7 +121,7 @@ describe('Log', () => {

Log.v(message)

expect(logManagerSpy).toHaveBeenCalledWith(message, '')
expect(logManagerSpy).toHaveBeenCalledWith(message, undefined)
})
})

Expand Down
12 changes: 4 additions & 8 deletions test/log/LogManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

Expand Down

0 comments on commit 43d28fc

Please sign in to comment.