Skip to content

Instantly share code, notes, and snippets.

@SgtPooki
Created February 28, 2022 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SgtPooki/1dbe94f414324d313e60d4102683dde9 to your computer and use it in GitHub Desktop.
Save SgtPooki/1dbe94f414324d313e60d4102683dde9 to your computer and use it in GitHub Desktop.
A simple console log wrapper
type Console = typeof console
/**
* This class' sole purpose is to avoid cluttering the codebase with `eslint-disable-line no-console` comments
*
* When using this class to log errors or messages, one can assume it's intentional and principled.
*/
class Log {
constructor (private readonly namespace?: string) {}
/**
* The log method's generic typing allows it to only accept
*
* @param method - log
* @param args
*/
private log<M extends Extract<keyof Console, keyof Omit<Log, 'log'>>>(method: M, ...args: Parameters<Console[M]>) {
const [msg, optionalParams] = args
const prefix = this.namespace != null ? `${this.namespace}: ` : ''
// eslint-disable-next-line no-console
console[method](`${prefix}${msg as string}`, optionalParams)
}
debug (...args: Parameters<Console['debug']>) {
this.log('debug', ...args)
}
info (...args: Parameters<Console['info']>) {
this.log('info', ...args)
}
warn (...args: Parameters<Console['warn']>) {
this.log('warn', ...args)
}
error (...args: Parameters<Console['error']>) {
this.log('error', ...args)
}
}
export { Log }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment