SHARE
TWEET

Untitled

a guest Oct 13th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type loggerLevel = 'info' | 'error' | 'warn' | 'debug';
  2.  
  3. const log = (message: string, level: loggerLevel = 'info') => {
  4.   console.log(`${new Date().toLocaleString()} [${level}] ${message}`)
  5. }
  6.  
  7. /**
  8.  * Create Logger with stopwatch
  9.  * @param label - stopwatch logger label
  10.  * @returns - logger wrapper with stopwatch feature
  11.  */
  12. export const stopwatchLoggerFactory = function createLoggerWithStopwatch(label: string) {
  13.   const startTime = new Date().getTime();
  14.   let prevTime = startTime;
  15.  
  16.   log(`Initialize stopwatch ${label}`)
  17.  
  18.   return (message: string, level: loggerLevel = 'info') => {
  19.     const currentTime = new Date().getTime();
  20.     const fromStart = currentTime - startTime;
  21.     const fromPrev = currentTime - prevTime;
  22.     const resultMessage = `${msToPrettyTime(fromPrev)} [${label}] ${message} (${msToPrettyTime(fromStart)})`;
  23.     log(resultMessage, level);
  24.     prevTime = currentTime;
  25.   };
  26. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top