Created
May 23, 2025 04:21
-
-
Save ribeiroevandro/e4a2b6b64ea955cc986d3cded33a6897 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { format } from 'date-fns'; | |
interface InfoProps { | |
tag: string; | |
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | |
content: any; | |
} | |
interface ErrorProps { | |
tag: string; | |
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | |
content: any; | |
} | |
interface TronDisplayProps { | |
name: string; | |
value?: object | string | number | boolean | null | undefined; | |
image?: string | { uri: string }; | |
important?: boolean; | |
} | |
type LogLevel = 'info' | 'error'; | |
interface LogProps { | |
level: LogLevel; | |
content: string; | |
} | |
const logColorsMap = (content: string) => ({ | |
info: `\u001b[44m\u001b[30m${content}\u001b[0m`, | |
error: `\u001b[41m\u001b[30m${content}\u001b[0m`, | |
}); | |
class TronLogger { | |
constructor(private logger: Logger) {} | |
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | |
public log(value: any) { | |
// console.tron.log(value); | |
} | |
public display({ name, value, image, important = true }: TronDisplayProps) { | |
console.tron.display({ | |
name, | |
value, | |
preview: name, | |
image, | |
important, | |
}); | |
} | |
} | |
class Logger { | |
public tron: TronLogger; | |
constructor() { | |
this.tron = new TronLogger(this); | |
} | |
public formatLog(tag: string, content: string) { | |
return `[${this.getCurrentTime()}] - [${tag}]: ${content}`; | |
} | |
private getCurrentTime() { | |
const currentDate = new Date(); | |
const milliseconds = currentDate.getMilliseconds(); | |
return `${format(currentDate, 'hh:mm:ss')}.${milliseconds}`; | |
} | |
public log({ level, content }: LogProps) { | |
console.log(`${logColorsMap(content)[level]}`); | |
} | |
public info({ tag, content }: InfoProps) { | |
const serializedContent = JSON.stringify(content); | |
this.log({ | |
level: 'info', | |
content: this.formatLog(tag, serializedContent), | |
}); | |
} | |
public error({ tag, content }: ErrorProps) { | |
const serializedContent = JSON.stringify(content); | |
this.log({ | |
level: 'error', | |
content: this.formatLog(tag, serializedContent), | |
}); | |
} | |
} | |
export const logger = new Logger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment