onNewLog method

Future<void> onNewLog(
  1. LogRecord logRecord
)

Method called upon Logger's 'onRecord'. When in editor mode, logs will be sent to the Editor.

Implementation

Future<void> onNewLog(LogRecord logRecord) async {
  if (editorMode) {
    if (logRecord.object is LogRecord) {
      // Created via extension methods 'infoWithContext', 'warningWithContext', etc.
      logRecord = logRecord.object as LogRecord;
    }

    Map? context = {};
    if (logRecord.object is Map) {
      for (var key in (logRecord.object as Map).keys) {
        var val = (logRecord.object as Map)[key];
        if (val is! Function && key != "null") {
          context[key] = val;
        }
      }
    }

    try {
      EditorBloc.instance?.add(LogEvent(
        logRecord.level.name,
        "${logRecord.time} [${logRecord.loggerName}] ${logRecord.message}",
        context: context,
        error: logRecord.error,
        stackTrace: logRecord.stackTrace,
      ));
    } catch (e) {
      // Do nothing.
    }
  }
}