JSFiddle - React, Tailwind, and code Playground
by Danny Michaelis
JavaScript
let oldLog;
class TagLogger {
constructor ( overwrite, ...tags )
{
if ( overwrite ) {
console.tags = new Set( tags );
oldLog = console.log;
console._log = console.log;
for( let attr of Object.getOwnPropertyNames( TagLogger.prototype ) ) {
console._log(attr, TagLogger.prototype[ attr ] )
console[ attr ] = TagLogger.prototype[ attr ];
}
} else {
this.tags = new Set( tags );
}
return this;
}
reset() {
const TL = new TagLogger( false, ...console.tags );
if( console.tags ) {
console.log = oldLog;
console.addTag = null;
console.pretty = null;
console.if = null;
console.tag = null;
console.tags = null;
}
return TL;
}
addTag( tag )
{
this.tags[tag] = true;
return this
}
log(...args)
{
console._log(...args);
return this;
}
pretty( obj, replace = null, spacing = 4 )
{
if ( !obj ) return '';
if ( typeof obj === 'string' ) return obj;
console._log( JSON.stringify( obj, replace, spacing ) )
return this
}
if( bool ) // This is the name of a function, not an if-statment
{
return {
then: ( ...args ) => {
if ( bool ) console.log( ...args );
return {
else: ( ...otherArgs ) => { if ( !bool ) console.log( ...otherArgs ) }
}
}
}
}
tag(...tags)
{
// if (tags.some((tag) => this.tags[tag])) return this
if ( tags.some( tag => this.tags.has( tag ) ) ) return this;
return {
log: (...args) => this,
pretty: (...args) => this,
}
}
}
const x = new TagLogger( 'x', 'y' )
console.pretty( console ).log( 'pretty, ain\'t it?');
console.tag('a').log('no').log( 'maybe?'...