Skip to content

Instantly share code, notes, and snippets.

@jakekara
Created October 7, 2020 14:08
Show Gist options
  • Save jakekara/b8eecd6eb2c71c56b11189e0b4b3b20d to your computer and use it in GitHub Desktop.
Save jakekara/b8eecd6eb2c71c56b11189e0b4b3b20d to your computer and use it in GitHub Desktop.
A quick wrapper for console.log .warn .error functions to improve devtools debugging
/**
* A cheap and cheerful logger that wraps console.log, .warn, .error, with
* labeling and silencing to make debugging with devtools easier.
*/
export default class DebugLogger {
private _label: string;
private _silent: boolean;
constructor(label: string) {
this._label = label;
this._silent = false;
// bind our workhorse functions
this.hush = this.hush.bind(this);
this.console = this.console.bind(this);
// bind our helper functions
this.log = this.log.bind(this);
this.warn = this.warn.bind(this);
this.error = this.error.bind(this);
}
hush() {
this._silent = true;
return this;
}
console(fname: "log" | "error" | "warn", ...args: any[]) {
if (this._silent) { return }
let f = console[fname];
f(`${this._label}:`, ...args);
}
log = (...args: any[]) => this.console("log", ...args);
warn = (...args: any[]) => this.console("warn", ...args);
error = (...args: any[]) => this.console("error", ...args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment