log method

  1. @override
List<String> log(
  1. LogEvent event
)

Is called every time a new LogEvent is sent and handles printing or storing the message.

Implementation

@override
List<String> log(LogEvent event) {
  if (EnumHelper.getIndexOf(Level.values, Log._loggingLevel) >
      EnumHelper.getIndexOf(Level.values, event.level)) {
    // don't log events where the log level is set higher
    return <String>[];
  }
  DateFormat formatter = DateFormat('yyyy-MM-dd HH:mm:ss.');
  DateTime now = DateTime.now();
  String formattedDate = formatter.format(now) + now.millisecond.toString();

  AnsiColor color = _getLevelColor(event.level);

  StackTraceNJ frames = StackTraceNJ();
  int i = 0;
  int depth = 0;
  if (frames.frames != null)
    for (Stackframe frame in frames.frames!) {
      i++;
      String path2 = frame.sourceFile.path;
      if (!path2.contains(Log._localPath) && !path2.contains('logger.dart')) {
        depth = i - 1;
        break;
      }
    }

  print(color(
      '[$formattedDate] ${event.level} ${StackTraceNJ(skipFrames: depth).formatStackTrace(methodCount: 1)} ::: ${event.message}'));
  if (event.error != null) {
    print('${event.error}');
  }

  if (event.stackTrace != null) {
    if (event.stackTrace.runtimeType == StackTraceNJ) {
      StackTraceNJ st = event.stackTrace as StackTraceNJ;
      print(color('$st'));
    } else {
      print(color('${event.stackTrace}'));
    }
  }

  return <String>[];
}