Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging large strings from Flutter

Tags:

flutter

dart

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?

like image 729
Terrabythia Avatar asked Mar 06 '18 19:03

Terrabythia


8 Answers

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]

like image 164
Sisir Avatar answered Nov 18 '22 14:11

Sisir


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

like image 30
CopsOnRoad Avatar answered Nov 18 '22 14:11

CopsOnRoad


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

debugPrint(someSuperLongString, wrapWidth: 1024);
like image 20
jesobremonte Avatar answered Nov 18 '22 14:11

jesobremonte


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));
       }
    }
}
like image 17
Bisma Frühling Avatar answered Nov 18 '22 14:11

Bisma Frühling


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

debugPrint and print are actually truncating the output.

like image 3
Tilo Avatar answered Nov 18 '22 14:11

Tilo


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.

like image 3
Ilo Calistus Avatar answered Nov 18 '22 14:11

Ilo Calistus


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);
like image 2
ch271828n Avatar answered Nov 18 '22 14:11

ch271828n


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.

like image 2
Mahi Avatar answered Nov 18 '22 13:11

Mahi