ansiLogMessageColorDecorator function

LogDecorator ansiLogMessageColorDecorator({
  1. Map<int, AnsiConsoleStyle> colorMap = _defaultColorMapper,
})

ansi(console) log color decorator that uses a map of log-level to AnsiColor to print a colored message based on log-level of record

the map must follow the following conditions

Implementation

LogDecorator ansiLogMessageColorDecorator({
  Map<int, AnsiConsoleStyle> colorMap = _defaultColorMapper,
}) {
  assert(
    () {
      final items = colorMap.keys.toList();
      for (var index = 1; index < items.length; index++) {
        if (items[index] >= items[index - 1]) {
          return false;
        }
      }
      return true;
    }(),
    'the map keys must be in descending order',
  );
  return (message, record) {
    final colorWrapper = colorMap.entries
        .where(
          (element) => element.key <= record.level,
        )
        .firstOrNull;
    if (colorWrapper == null) {
      return message;
    } else {
      return colorWrapper.value.wrap(message);
    }
  };
}