prettyLog function

void prettyLog(
  1. LogRecord record, {
  2. bool omitError(
    1. LogRecord
    )?,
  3. void printFunction(
    1. String
    )?,
  4. AnsiCode logColorChooser(
    1. Level
    )?,
})

Prints the contents of a LogRecord with pretty colors.

By passing omitError, you can omit printing the error of a given LogRecord.

You can also pass a custom printFunction or logColorChooser.

Implementation

void prettyLog(LogRecord record,
    {bool Function(LogRecord)? omitError,
    void Function(String)? printFunction,
    AnsiCode Function(Level)? logColorChooser}) {
  logColorChooser ??= chooseLogColor;
  omitError ??= (_) => false;
  printFunction ??= print;

  var code = logColorChooser(record.level);
  if (record.error == null) printFunction(code.wrap(record.toString())!);

  if (record.error != null) {
    var err = record.error;
    if (omitError(record)) return;
    printFunction(code.wrap(record.toString() + '\n')!);
    printFunction(code.wrap(err.toString())!);

    if (record.stackTrace != null) {
      printFunction(code.wrap(record.stackTrace.toString())!);
    }
  }
}