Advertisement
Jragyn

Colorful JS console.log wrapper

Jul 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // small wrapper of console.log
  2. //   msg  = string of text you want to output
  3. //   type = some string matching a case in the switch
  4. // result: will color and style your console.log messages.
  5. // note: if no type is added, 'default' will be used.
  6. function Log(msg, type) {
  7.   if (type === undefined || type === null) type = 'default';
  8.   var styles = '';
  9.   styles += 'text-shadow: 2px 2px rgba(0, 0, 0, 0.8);';
  10.   styles += 'font-size: 14px;';
  11.   styles += 'font-family: "Consolas";';
  12.   styles += 'border: 2px solid black;';
  13.   styles += 'line-height: 20px;';
  14.   styles += 'width: 100%;'
  15.   switch (type) {
  16.     case 'default':
  17.       // no concat of messages.
  18.       styles += 'color: white;';
  19.       styles += 'background: darkslategray;';
  20.       break;
  21.     case 'ok':
  22.       msg = "     OK: " + msg;
  23.       styles += 'color: ghostwhite;';
  24.       styles += 'background: green;';
  25.       break;
  26.     case 'err':
  27.       msg = "  ERROR: " + msg;
  28.       styles += 'color: gold;';
  29.       styles += 'background: firebrick;';
  30.       styles += 'font-weight: bold;';
  31.       break;
  32.     case 'net':
  33.       msg = "NETWORK: " + msg;
  34.       styles += 'color: palegreen;';
  35.       styles += 'background: rebeccapurple;';
  36.       styles += 'font-style: italic;';
  37.       break;
  38.     //case '???':
  39.       // uncomment this case and
  40.       // add stuff here if you want more cases!
  41.       //break;
  42.     default: break;
  43.   }
  44.   console.log('%c ' + msg + " ", styles);
  45. }
  46.  
  47. Log("regular message with no specification of type.");
  48. Log("an OK typed message.", 'ok');
  49. Log("an ERR typed message.", 'err');
  50. Log("a NET typed message.", 'net');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement