DEV Community

Discussion on: Better console.logs

Collapse
 
link2twenty profile image
Andrew Bone

With your code, you can actually have it change the console type too, like so

function customLog(message, color='black') {
     let logType = 'log';
     switch (color) {
         case 'success':  
              color = 'Green';
              break
         case 'info':     
              color = 'Blue';
              logType = 'info';
              break;
         case 'error':   
              color = 'Red'; 
              logType = 'error';
              break;
         case 'warning':  
              color = 'Orange';
              logType = 'warning';
              break;
         default: 
              color = color
     }

     console[logType](`%c${message}`, `color:${color}`);
}

customLog('Hello World!')
customLog('Success!', 'success')
customLog('Error!', 'error')
customLog('Warning!', 'warning')
customLog('Info...', 'info')

Also, console.count('nameToCount') will count how many times it is called, which can be handy.