1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-06-12 19:09:18 +00:00

Added base support for object

This commit is contained in:
2020-07-01 10:48:43 +02:00
parent e57c934edb
commit bea311ae50
3 changed files with 59 additions and 8 deletions

View File

@ -1,23 +1,43 @@
import chalk from 'chalk'
import { white, blue, yellow, green } from 'chalk'
/**
* Logger Class
*/
export default class Logger {
/**
* If it is set to true all message won't be shown until it is set to false
*/
public static isBlocked = false
private static queue: Array<string> = []
private static queue: Array<Array<any>> = []
private static prefixLen = 0
/**
* Log a message into the console
* @param prefix the prefix used
* @param message the message to log
*/
public static log(prefix: string, ...message: Array<any>) {
this.queue.push(this.formatMessage(prefix, ...message))
while (this.queue.length > 0 && !this.isBlocked) {
console.log(this.queue.shift())
const item = this.queue.shift()
if (!item) {
continue
}
console.log(...item)
}
}
public static urgent(prefix: string, ...message: Array<any>) {
/**
* Log a message into the console (passthrough the `Logger.isBlocked` boolean)
* @param prefix The prefix used
* @param message the message to log
*/
public static urgent(prefix: string, ...message: Array<any>) {
console.log(this.formatMessage(prefix, ...message))
}
private static formatMessage(prefix: string, ...message: Array<any>): string {
private static formatMessage(prefix: string, ...message: Array<any>): Array<any> {
if (this.prefixLen < prefix.length) {
this.prefixLen = prefix.length
}
@ -27,7 +47,17 @@ export default class Logger {
els[0] = this.buildSpace(diff / 2 - (diff % 2 !== 0 ? 1 : 0))
els[1] = this.buildSpace(diff / 2)
}
return `${chalk.white('[ ')}${els[0]}${chalk.blue(prefix)}${els[1]}${chalk.white(' ]')}: ${message.map((el) => typeof el === 'number' ? chalk.yellow(el.toString()) : chalk.white(el)).join(' ')}`
const res: Array<any> = [
`${white('[ ')}${els[0]}${blue(prefix)}${els[1]}${white(' ]')}:` // prefix
].concat(
message.map((el) => {
if (typeof el === 'object') {
return el
}
return typeof el !== 'string' ? yellow(el.toString()) : green(el)
})
)
return res
}
private static buildSpace(count: number): string {

View File

@ -14,4 +14,4 @@ module.exports = {
}
]
}
};
}