chore: vault tests refactors and lint (#2374)
This commit is contained in:
1
packages/utils/src/Domain/Logger/LogLevel.ts
Normal file
1
packages/utils/src/Domain/Logger/LogLevel.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none'
|
||||
64
packages/utils/src/Domain/Logger/Logger.ts
Normal file
64
packages/utils/src/Domain/Logger/Logger.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import { LogLevel } from './LogLevel'
|
||||
|
||||
export class Logger {
|
||||
private level: LogLevel = 'none'
|
||||
|
||||
constructor(private appIdentifier: string) {}
|
||||
|
||||
private canLog(level: LogLevel): boolean {
|
||||
if (this.level === 'none') {
|
||||
return false
|
||||
}
|
||||
|
||||
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error']
|
||||
return levels.indexOf(level) >= levels.indexOf(this.level)
|
||||
}
|
||||
|
||||
public setLevel(level: LogLevel): void {
|
||||
this.level = level
|
||||
}
|
||||
|
||||
public debug(message: string, ...optionalParams: any[]): void {
|
||||
if (this.canLog('debug')) {
|
||||
this.logWithColor(message, ...optionalParams)
|
||||
}
|
||||
}
|
||||
|
||||
public info(message: string, ...optionalParams: any[]): void {
|
||||
if (this.canLog('info')) {
|
||||
this.logWithColor(message, ...optionalParams)
|
||||
}
|
||||
}
|
||||
|
||||
public warn(message: string, ...optionalParams: any[]): void {
|
||||
if (this.canLog('warn')) {
|
||||
console.warn(message, ...optionalParams)
|
||||
}
|
||||
}
|
||||
|
||||
public error(message: string, ...optionalParams: any[]): void {
|
||||
if (this.canLog('error')) {
|
||||
console.error(message, ...optionalParams)
|
||||
}
|
||||
}
|
||||
|
||||
private logWithColor(...args: any[]): void {
|
||||
const date = new Date()
|
||||
const timeString = `${date.toLocaleTimeString().replace(' PM', '').replace(' AM', '')}.${date.getMilliseconds()}`
|
||||
this.customLog(
|
||||
`%c${this.appIdentifier}%c${timeString}`,
|
||||
'color: font-weight: bold; margin-right: 4px',
|
||||
'color: gray',
|
||||
...args,
|
||||
)
|
||||
}
|
||||
|
||||
private customLog(..._args: any[]) {
|
||||
// eslint-disable-next-line no-console, prefer-rest-params
|
||||
Function.prototype.apply.call(console.log, console, arguments)
|
||||
}
|
||||
}
|
||||
|
||||
export default Logger
|
||||
10
packages/utils/src/Domain/Logger/LoggerInterface.ts
Normal file
10
packages/utils/src/Domain/Logger/LoggerInterface.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { LogLevel } from './LogLevel'
|
||||
|
||||
export interface LoggerInterface {
|
||||
setLevel(level: LogLevel): void
|
||||
debug(message: string, ...optionalParams: any[]): void
|
||||
info(message: string, ...optionalParams: any[]): void
|
||||
warn(message: string, ...optionalParams: any[]): void
|
||||
error(message: string, ...optionalParams: any[]): void
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
export * from './Date/DateUtils'
|
||||
export * from './Deferred/Deferred'
|
||||
export * from './Logger/Logger'
|
||||
export * from './Logger/LoggerInterface'
|
||||
export * from './Logger/LogLevel'
|
||||
export * from './Utils/ClassNames'
|
||||
export * from './Utils/Utils'
|
||||
export * from './Utils/Debounce'
|
||||
export * from './Utils/Utils'
|
||||
export * from './Uuid/Utils'
|
||||
export * from './Uuid/UuidGenerator'
|
||||
export * from './Uuid/UuidMap'
|
||||
|
||||
Reference in New Issue
Block a user