mirror of
https://github.com/dzeiocom/libs.git
synced 2025-06-14 19:59:17 +00:00
Initial commit With Logger
This commit is contained in:
40
packages/logger/src/Logger.ts
Normal file
40
packages/logger/src/Logger.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import chalk from 'chalk'
|
||||
|
||||
export default class Logger {
|
||||
public static isBlocked = false
|
||||
private static queue: Array<string> = []
|
||||
private static prefixLen = 0
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
public static urgent(prefix: string, ...message: Array<any>) {
|
||||
|
||||
console.log(this.formatMessage(prefix, ...message))
|
||||
}
|
||||
|
||||
private static formatMessage(prefix: string, ...message: Array<any>): string {
|
||||
if (this.prefixLen < prefix.length) {
|
||||
this.prefixLen = prefix.length
|
||||
}
|
||||
const els: Array<string> = ['', '']
|
||||
if (this.prefixLen > prefix.length) {
|
||||
const diff = this.prefixLen - prefix.length
|
||||
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(' ')}`
|
||||
}
|
||||
|
||||
private static buildSpace(count: number): string {
|
||||
let str = ''
|
||||
for(let i = 0; i < count; i++) {
|
||||
str += ' '
|
||||
}
|
||||
return str
|
||||
}
|
||||
}
|
6
packages/logger/src/index.ts
Normal file
6
packages/logger/src/index.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import Logger from './Logger'
|
||||
|
||||
// Browser Import
|
||||
|
||||
// @ts-ignore
|
||||
window.Logger = Logger
|
Reference in New Issue
Block a user