Introduce a new ConsoleLogger JS class

Extract debug logic to separate component
This commit is contained in:
Philipp Stracker 2024-08-02 16:32:27 +02:00
parent 9da37a2cc6
commit f1f243505c
No known key found for this signature in database
2 changed files with 56 additions and 29 deletions

View file

@ -0,0 +1,42 @@
/**
* Logs debug details to the console.
*
* A utility class that is used by payment buttons on the front-end, like the GooglePayButton.
*/
export default class ConsoleLogger {
/**
* The prefix to display before every log output.
*
* @type {string}
*/
#prefix = '';
/**
* Whether logging is enabled, disabled by default.
*
* @type {boolean}
*/
#enabled = false;
constructor( ...prefixes ) {
if ( prefixes.length ) {
this.#prefix = `[${ prefixes.join( ' | ' ) }]`;
}
}
set enabled( state ) {
this.#enabled = state;
}
log( ...args ) {
if ( this.#enabled ) {
console.log( this.#prefix, ...args );
}
}
error( ...args ) {
if ( this.#enabled ) {
console.error( this.#prefix, ...args );
}
}
}