Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

1.2 KB
Raw Blame
import chalk from 'chalk';
import { identity } from 'lodash-es';

export const newLineSeparator = /\r\n|[\n\r\x85\u2028\u2029]/g;

interface StringLike {
  toString: () => string;
}

export class Logger {
  log = this.getLineLogger(console.log.bind(console));
  info = this.getLineLogger(console.info.bind(console), chalk.blue);
  warn = this.getLineLogger(
    console.warn.bind(console),
    chalk.bgHex('#322b08').hex('#fadea6')
  );
  error = this.getLineLogger(
    console.error.bind(console),
    chalk.bgHex('#250201').hex('#ef8784')
  );
  success = this.getLineLogger(console.log.bind(console), chalk.green);

  constructor(private readonly tag: string = '') {}

  getLineLogger(
    logLine: (...line: string[]) => void,
    color: (...text: string[]) => string = identity
  ) {
    return (...args: StringLike[]) => {
      args.forEach(arg => {
        arg
          .toString()
          .split(newLineSeparator)
          .forEach(line => {
            if (line.length !== 0) {
              if (this.tag) {
                logLine(color(`[${this.tag}] ${line}`));
              } else {
                logLine(color(line));
              }
            }
          });
      });
    };
  }
}