Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // usage:
- const logger = new Logger();
- logger.log(
- class Logger {
- constructor() {
- this.render = ({ title, out = console.log, color = chalk.reset }) => (...args) => {
- const shortStack = e =>
- e instanceof Error
- ? e.stack
- .split('\n')
- .slice(0, 2)
- .join('\n')
- : e;
- const log = out(color(shortStack()));
- const group = args.length > 1;
- const file = () => {
- const entry = new Error().stack.split('\n')[1];
- return entry ? (entry.match(/([^\\/]+)\)/) || [])[1] : '';
- };
- if (args.length > 1) {
- if (group) console.group(color(title), file());
- args.forEach(log);
- if (group) console.groupEnd();
- } else {
- out(color(args), file());
- }
- };
- this.log = this.render({ title: 'LOG:' });
- this.debug = this.render({ title: 'DEBUG:', log: console.debug, color: chalk.blue });
- this.warn = this.render({ title: 'WARN:', log: console.warn, color: chalk.yellow });
- this.error = this.render({ title: 'ERR:', log: console.error, color: chalk.red });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement