defaultLogObjectEncoder function

Object? defaultLogObjectEncoder(
  1. Object value
)

Default encoder for common Dart objects that JSON cannot represent.

Every result carries a $type marker so the log does not silently present the object as a JSON-native string or number.

Implementation

Object? defaultLogObjectEncoder(Object value) {
  if (value is FormData) {
    return _TaggedLogValue('FormData', {
      'boundary': value.boundary,
      'isFinalized': value.isFinalized,
      'fields': [
        for (final field in value.fields)
          <String, Object>{
            'key': field.key,
            'value': field.value,
          },
      ],
      'files': [
        for (final file in value.files)
          <String, Object>{
            'key': file.key,
            'value': file.value,
          },
      ],
    });
  }

  if (value is MultipartFile) {
    return _TaggedLogValue('MultipartFile', {
      'filename': value.filename,
      'length': value.length,
      'contentType': value.contentType?.toString(),
      'headers': value.headers,
      'isFinalized': value.isFinalized,
    });
  }

  if (value is DateTime) {
    return _TaggedLogValue('DateTime', {
      'value': value.toIso8601String(),
      'microsecondsSinceEpoch': value.microsecondsSinceEpoch,
      'isUtc': value.isUtc,
      'timeZoneOffsetMicroseconds': value.timeZoneOffset.inMicroseconds,
    });
  }

  if (value is Uri) {
    return _TaggedLogValue('Uri', {
      'value': value.toString(),
    });
  }

  if (value is Duration) {
    return _TaggedLogValue('Duration', {
      'microseconds': value.inMicroseconds,
      'value': value.toString(),
    });
  }

  if (value is BigInt) {
    return _TaggedLogValue('BigInt', {
      'value': value.toString(),
    });
  }

  if (value is RegExp) {
    return _TaggedLogValue('RegExp', {
      'pattern': value.pattern,
      'isCaseSensitive': value.isCaseSensitive,
      'isMultiLine': value.isMultiLine,
      'isUnicode': value.isUnicode,
      'dotAll': value.isDotAll,
    });
  }

  if (value is Enum) {
    return _TaggedLogValue(value.runtimeType.toString(), {
      'name': value.name,
      'index': value.index,
    });
  }

  if (value is Type) {
    return _TaggedLogValue('Type', {
      'value': value.toString(),
    });
  }

  if (value is Error) {
    return _TaggedLogValue(value.runtimeType.toString(), {
      'message': _safeObjectToString(value),
      if (value.stackTrace != null) 'stackTrace': value.stackTrace.toString(),
    });
  }

  if (value is Exception || value is StackTrace || value is Symbol) {
    return _TaggedLogValue(value.runtimeType.toString(), {
      'value': _safeObjectToString(value),
    });
  }

  return _TaggedLogValue(value.runtimeType.toString(), {
    'value': _safeObjectToString(value),
  });
}