Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // usage:
  2. const logger = new Logger();
  3. logger.log(
  4.  
  5. class Logger {
  6.   constructor() {
  7.     this.render = ({ title, out = console.log, color = chalk.reset }) => (...args) => {
  8.       const shortStack = e =>
  9.         e instanceof Error
  10.           ? e.stack
  11.               .split('\n')
  12.               .slice(0, 2)
  13.               .join('\n')
  14.           : e;
  15.       const log = out(color(shortStack()));
  16.       const group = args.length > 1;
  17.       const file = () => {
  18.         const entry = new Error().stack.split('\n')[1];
  19.         return entry ? (entry.match(/([^\\/]+)\)/) || [])[1] : '';
  20.       };
  21.  
  22.       if (args.length > 1) {
  23.         if (group) console.group(color(title), file());
  24.         args.forEach(log);
  25.         if (group) console.groupEnd();
  26.       } else {
  27.         out(color(args), file());
  28.       }
  29.     };
  30.  
  31.     this.log = this.render({ title: 'LOG:' });
  32.     this.debug = this.render({ title: 'DEBUG:', log: console.debug, color: chalk.blue });
  33.     this.warn = this.render({ title: 'WARN:', log: console.warn, color: chalk.yellow });
  34.     this.error = this.render({ title: 'ERR:', log: console.error, color: chalk.red });
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement