This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
works-on-my-machine/src/logging.ts
Marc Cataford e216c2d04e
refactor: replace console by logging util (#27)
* refactor: replace console by logging util

* refactor: more logging replacements

* refactor: more logging replacements
2023-04-15 22:18:27 -04:00

46 lines
1 KiB
TypeScript

import { redText, greenText, yellowText } from './utils'
/*
* Standard logger for anything that needs to print messages to the user.
*
* This supports the same general functionality as the `Console` logger,
* including `group` and various levels of logging.
*/
class Logger {
indent: number = 0
get #indentPrefix(): string {
return ' '.repeat(this.indent)
}
#formatMessage(text: string): string {
return `[${new Date().toLocaleString()}] ${text}`
}
group(label: string) {
process.stdout.write(this.#formatMessage(`${label}\n`))
this.indent += 1
}
groupEnd() {
if (this.indent > 0) this.indent -= 1
}
logError(text: string) {
process.stdout.write(this.#formatMessage(`${this.#indentPrefix}${redText(text)}\n`))
}
logWarning(text: string) {
process.stdout.write(`${this.#indentPrefix}${yellowText(text)}\n`)
}
log(text: string) {
process.stdout.write(this.#formatMessage(`${this.#indentPrefix}${text}\n`))
}
logRaw(text: string) {
process.stdout.write(`${text}\n`)
}
}
export default () => new Logger()