New ConsoleLogger group method

Groups subsequent console.log output, for cleaner console output
This commit is contained in:
Philipp Stracker 2024-08-16 18:15:59 +02:00
parent 00e2959700
commit 63e9c8bf27
No known key found for this signature in database
3 changed files with 49 additions and 7 deletions

View file

@ -18,6 +18,13 @@ export default class ConsoleLogger {
*/
#enabled = false;
/**
* Tracks the current log-group that was started using `this.group()`
*
* @type {?string}
*/
#openGroup = null;
constructor( ...prefixes ) {
if ( prefixes.length ) {
this.#prefix = `[${ prefixes.join( ' | ' ) }]`;
@ -55,4 +62,28 @@ export default class ConsoleLogger {
error( ...args ) {
console.error( this.#prefix, ...args );
}
/**
* Starts or ends a group in the browser console.
*
* @param {string} [label=null] - The group label. Omit to end the current group.
*/
group( label = null ) {
if ( ! this.#enabled ) {
return;
}
if ( ! label || this.#openGroup ) {
// eslint-disable-next-line
console.groupEnd();
this.#openGroup = null;
}
if ( label ) {
// eslint-disable-next-line
console.group( label );
this.#openGroup = label;
}
}
}