All files yago.logger.ts

88.89% Statements 24/27
71.43% Branches 10/14
87.5% Functions 7/8
88.89% Lines 24/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                7x             7x 10x 10x 10x 10x                     2x 2x 2x 2x               7x 7x               7x 7x 7x     7x 7x               7x                           161x 118x 118x   43x 43x 43x                                                        
// Debugger in the browser console can be controlled through a url query parameter.
// For example: http://localhost:3000?logger=true
 
/**
 * Controls default logger behaviour.
 * Pass a 'false' value to avoid logger. This is the desired behaviour for production.
 * @type {string}
 */
const DEFAULT_LOG_STATE: string = 'false';
 
/**
 * Gets url query parameter form URL
 * @param {string} paramName
 * @returns {string}
 */
export const getUrlParameter = (paramName: string) => {
  paramName = paramName.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
  const regex = new RegExp('[\\?&]' + paramName + '=([^&#]*)');
  const results = regex.exec(location.search);
  return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
 
/**
 * Update query string parameter
 * @param {string} uri
 * @param {string} key
 * @param {string} value
 * @return {string}
 */
export function updateQueryStringParameter(uri: string, key: string, value: string) {
  const re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  const separator = uri.indexOf('?') !== -1 ? "&" : "?";
  Eif (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}
 
// Variable that holds the looger state
let urlLogParam = getUrlParameter('logger');
urlLogParam = urlLogParam || DEFAULT_LOG_STATE;
 
/**
 * Disables console output messages (except error).
 * Used for production mode
 */
export function disableConsole(): void {
  // console = console || {};
  console.log = () => {};
  console.table = () => {};
  console.warn = () => {};
}
 
Eif (urlLogParam === 'false') {
  disableConsole();
}
 
/**
 * Logs message formats: foreground color, background color
 * CSS syntax is used to format messages
 * @type {Object}
 */
export const LOG_FORMAT = {
  BLUE: 'background: ghostwhite; color: cornflowerblue; font-size: 12px; font-weight: bold',
  ORANGE: 'color: orange',
  BG_YELLOW: 'background-color: yellow'
};
 
/**
 * Logs sql query strings and query results, appling two formats:
 * Input (query): blue colors
 * Output (result): yellow colors
 * @param {string} msg
 * @param {"query" | "result" | string} format
 */
export function logQuery(msg: string, format: 'query' | 'result' | string): void {
  if (format === 'query') {
    const format = LOG_FORMAT.BLUE;
    console.log(`%c${msg}`, format);
  } else
  Eif (format === 'result') {
    const format = LOG_FORMAT.ORANGE;
    console.log(`%c✅ ${msg}`, format);
  } else {
    console.log(`%c${msg}`, format)
  }
}
 
/**
 * General Fn to apply format to a log message
 * @param {string} msg
 * @param {string} format
 */
export function log(msg: string, format: string): void {
  console.log(`%c${msg}`, format);
}
 
/* Using a global variable: ********************************** */
 
// interface window {
//   DEBUG: boolean;
// }
//
// export const setDebugLevel = (flag: boolean = true): void => {
//   (window as any).DEBUG = flag;
// };
 
/* Get query string parameters for modern browsers. Doesnt work with Jest. A shim is needed */
// const urlParams = new URLSearchParams(window.location.search);
// let urlLogParam = urlParams.get('log');