Logging large strings from Flutter

DartFlutter

Dart Problem Overview


I'm trying to build a Flutter App and learning Dart in the process, but I'm getting kind of frustrated when debugging. I have fetched a resource from an API and now I want to print the JSON string to the console, but it keeps cutting off the string.

Screenshot of the cut off string in the console

So I actually have two questions: is the terminal console really the only way to print debug messages and how can I print large strings to the console without them automatically getting cut off?

Dart Solutions


Solution 1 - Dart

How about using the Flutter log from the dart: developer library. This does not seem to have the maximum length limit like print() or debugPrint(). This is the only solution that seems to work fine. Try it as below:

log(reallyReallyLongText)

The output will be the entire long string without breaks and prefixed with [log]

Solution 2 - Dart

You can make your own print. Define this method

void printWrapped(String text) {
  final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
  pattern.allMatches(text).forEach((match) => print(match.group(0)));
}

Use it like

printWrapped("Your very long string ...");

Credit

Solution 3 - Dart

Currently dart doesn't support printing logs more than 1020 characters (found that out by trying).

So, I came up with this method to print long logs:

static void LogPrint(Object object) async {
    int defaultPrintLength = 1020;
    if (object == null || object.toString().length <= defaultPrintLength) {
       print(object);
    } else {
       String log = object.toString();
       int start = 0;
       int endIndex = defaultPrintLength;
       int logLength = log.length;
       int tmpLogLength = log.length;
       while (endIndex < logLength) {
          print(log.substring(start, endIndex));
          endIndex += defaultPrintLength;
          start += defaultPrintLength;
          tmpLogLength -= defaultPrintLength;
       }
       if (tmpLogLength > 0) {
          print(log.substring(start, logLength));
       }
    }
}

Solution 4 - Dart

Use debugPrint with the optional parameter to wrap according to the platform's output limit.

debugPrint(someSuperLongString, wrapWidth: 1024);

Solution 5 - Dart

There is an open issue for that: https://github.com/flutter/flutter/issues/22665

debugPrint and print are actually truncating the output.

Solution 6 - Dart

Please try debugPrint('your output'); instead of print('your output'); the documentation is here if you would like to read. debugPrint throttles the output to a level to avoid being dropped by android's kernel as per the documentation.

Solution 7 - Dart

You can achieve this using the Logger Plugin: https://pub.dev/packages/logger

To print any type of log Just do the do the following.

  var logger = Logger();

  logger.d("Logger is working!");// It also accept json objects

In fact, it will even format the output for you.

Solution 8 - Dart

Use this method

JsonEncoder encoder = new JsonEncoder.withIndent('  ');
String prettyprint = encoder.convert(yourJsonString);
debugPrint(prettyprint);

But it works only with JSON strings, I have not idea about how to do with "normal" strings. The first thing I think is to add manually a \n after every n characters, for now, while waiting for a stable fix!

Solution 9 - Dart

Here is a one-liner based on @CopsOnRoad's answer that you can quickly copy and paste (such as: when you want to slightly modify your code and log some data and see temporarily):

void printWrapped(String text) => RegExp('.{1,800}').allMatches(text).map((m) => m.group(0)).forEach(print);

Solution 10 - Dart

If you run the application in android studio it will truncate long string.

In xcode 10.2 which i am using long string is not truncating.

My suggestion is write print statement logs and run the application in Xcode instead of android studio.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTerrabythiaView Question on Stackoverflow
Solution 1 - DartSisirView Answer on Stackoverflow
Solution 2 - DartCopsOnRoadView Answer on Stackoverflow
Solution 3 - DartBisma FrühlingView Answer on Stackoverflow
Solution 4 - DartjesobremonteView Answer on Stackoverflow
Solution 5 - DartTiloView Answer on Stackoverflow
Solution 6 - DartMahiView Answer on Stackoverflow
Solution 7 - DartIlo CalistusView Answer on Stackoverflow
Solution 8 - DartParesh MangukiyaView Answer on Stackoverflow
Solution 9 - Dartch271828nView Answer on Stackoverflow
Solution 10 - Dartyug kView Answer on Stackoverflow