Skip to content

fix long log details - #10011

Open
khanak0509 wants to merge 3 commits into
flutter:masterfrom
khanak0509:fix-logging-long-message-details-9596
Open

khanak0509 wants to merge 3 commits into
flutter:masterfrom
khanak0509:fix-logging-long-message-details-9596

Conversation

@khanak0509

Copy link
Copy Markdown
Contributor

Issue #9596
this PR fixes issue #9596 where long dart:developer.log messages lost their structured Details view. Short logs showed the full event tree, but long ones (like a JWT) only showed the plain message string.

How I fixed it:
basically, when the VM truncates a long message we still fetch the full string, but instead of replacing the details with that plain string we patch it back into the original event JSON. So Details stays structured for both short and long logs.

before --
before

after --
after1
after2
after3

Pre-launch Checklist

General checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read the Flutter Style Guide recently, and have followed its advice.
  • I signed the CLA.
  • I updated/added relevant documentation (doc comments with ///).

Issues checklist

Tests checklist

  • I added new tests to check the change I am making...
  • OR there is a reason for not adding tests, which I explained in the PR description.

AI-tooling checklist

  • I did not use any AI tooling in creating this PR.
  • OR I did use AI tooling, and...
    • I read the AI contributions guidelines and agree to follow them.
    • I reviewed all AI-generated code before opening this PR.
    • I understand and am able to discuss the code in this PR.
    • I have verifed the accuracy of any AI-generated text included in the PR description.
    • I commit to verifying the accuracy of any AI-generated code or text that I upload in response to review comments.

Feature-change checklist

  • This PR does not change the DevTools UI or behavior and...
    • I added the release-notes-not-required label or left a comment requesting the label be added.
  • OR this PR does change the DevTools UI or behavior and...
    • I added an entry to packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md.
    • I included before/after screenshots and/or a GIF demo of the new UI to my PR description.
    • I ran the DevTools app locally to manually verify my changes.

build.yaml badge

If you need help, consider asking for help on Discord.

@khanak0509
khanak0509 requested a review from a team as a code owner September 19, 2026 17:01
@khanak0509
khanak0509 requested review from srawlins and removed request for a team September 19, 2026 17:01

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors how developer log details are processed in the logging controller, moving from string concatenation to structured JSON manipulation via the new computeDeveloperLogDetailsJson helper, and adds corresponding unit tests. The review feedback identifies a performance concern regarding the use of jsonDecode(jsonEncode(...)) for deep copying on the UI thread, suggesting a more efficient copy-on-write approach to avoid UI lag.

Comment on lines +980 to +1014
final detailsJson = jsonDecode(jsonEncode(eventJson)) as Map<String, dynamic>;
final logRecord = detailsJson['logRecord'];
if (logRecord is Map<String, dynamic>) {
_applyFullStringToInstanceJson(logRecord['message'], fullMessage);

if (fullError != null) {
final errorJson = logRecord['error'];
if (errorJson is Map<String, dynamic> &&
errorJson['valueAsString'] != null) {
_applyFullStringToInstanceJson(errorJson, fullError);
} else {
logRecord['errorAsString'] = fullError;
}
}

if (fullStackTrace != null) {
final stackJson = logRecord['stackTrace'];
if (stackJson is Map<String, dynamic> &&
stackJson['valueAsString'] != null) {
_applyFullStringToInstanceJson(stackJson, fullStackTrace);
} else {
logRecord['stackTraceAsString'] = fullStackTrace;
}
}
}

return jsonEncode(detailsJson);
}

void _applyFullStringToInstanceJson(Object? instanceJson, String fullValue) {
if (instanceJson is! Map) return;
instanceJson['valueAsString'] = fullValue;
instanceJson['valueAsStringIsTruncated'] = false;
instanceJson['length'] = fullValue.length;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[CONCERN] Using jsonDecode(jsonEncode(eventJson)) to perform a deep copy of the log event can be a significant performance bottleneck, especially on the UI thread where DevTools runs, and particularly for large log payloads (such as JWTs or large JSON objects).

Instead of a full serialization/deserialization cycle, we can use a shallow copy-on-write approach for only the parts of the map that we actually modify. This avoids unnecessary CPU overhead and garbage collection pressure.

  final detailsJson = Map<String, dynamic>.from(eventJson);
  final logRecord = detailsJson['logRecord'];
  if (logRecord is Map) {
    final logRecordCopy = Map<String, dynamic>.from(logRecord);
    detailsJson['logRecord'] = logRecordCopy;

    logRecordCopy['message'] = _applyFullStringToInstanceJson(
      logRecordCopy['message'],
      fullMessage,
    );

    if (fullError != null) {
      final errorJson = logRecordCopy['error'];
      if (errorJson is Map && errorJson['valueAsString'] != null) {
        logRecordCopy['error'] = _applyFullStringToInstanceJson(
          errorJson,
          fullError,
        );
      } else {
        logRecordCopy['errorAsString'] = fullError;
      }
    }

    if (fullStackTrace != null) {
      final stackJson = logRecordCopy['stackTrace'];
      if (stackJson is Map && stackJson['valueAsString'] != null) {
        logRecordCopy['stackTrace'] = _applyFullStringToInstanceJson(
          stackJson,
          fullStackTrace,
        );
      } else {
        logRecordCopy['stackTraceAsString'] = fullStackTrace;
      }
    }
  }

  return jsonEncode(detailsJson);
}

Object? _applyFullStringToInstanceJson(Object? instanceJson, String fullValue) {
  if (instanceJson is! Map) return instanceJson;
  return <String, dynamic>{
    ...instanceJson,
    'valueAsString': fullValue,
    'valueAsStringIsTruncated': false,
    'length': fullValue.length,
  };
References
  1. Prioritize logic, performance on the UI thread, and architectural consistency. (link)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant