11

I am trying to create a util for logging.

I need to know the actual differences between print() vs debugPrint() and log().

Many answers I have seen are outdated or confusing. Also, some upvoted answers are contradict.

I am trying to print error messages in red color. I am using ANSI code to achieve it.

debugPrint("\x1B[31m HelloDebug \x1B[0m");

The above one print in red color.

But when i do the same using log(), its not printing in red color. Its escaping the ANSI code.

One thing I found out was log() has error parameter. If i pass something as error , it handles red color by default.

Here in the first one using log, red is not coming because ANSI code is ignored. But in debugPrint using it's working fine. In third one it takes error red by default. But extra line is added even for empty string message.

Code: enter image description here

Output: enter image description here

Is it possible use ANSI code for making color text using log? Because I have other ANSI code to make different color text.

I dont want use debugPrint because if I print some thing too frequently , the system ignore the logs.

After digging some time I found out that log function is implemented in c++ in the dart runtime.As the log function is external function in dart sdk.

external void log(
  String message, {
  DateTime? time,
  int? sequenceNumber,
  int level = 0,
  String name = '',
  Zone? zone,
  Object? error,
  StackTrace? stackTrace,
});

https://github.com/dart-lang/sdk/blob/main/runtime/lib/developer.cc

Also I see some different answers while digging. The below answer says debugPrint only available inside widget class. Is it still true?

https://stackoverflow.com/a/52241553/9248098

EDIT:

While using debugPrint and when I launch app from Android studio terminal, the ANSI color is working in android but when I run it same in iOS its escaping the characters in same Android Studio terminal.

If ANSI code support is based on terminal support, I couldn't figure out why its having issue in same terminal in iOS.

Gowtham K K
  • 3,123
  • 1
  • 11
  • 29

1 Answers1

5

It is well explained in this page Debugging Flutter apps programmatically

debugPrint() is similar to print() but with a throttles applied to prevents being dropped by Android’s kernel.

If you have linter setup on VSCode or other IDE, you will see this warning whenever you use print in your code. For more information, you can refer to Avoid print calls in production code.

enter image description here

To avoid the lint error, you can use kDebugMode with print or debugPrint instead.

if (kDebugMode) {
    print('test print');
}
// OR
debugPrint('test debugPrint');

As for log(), it actually allows you to include more information on the logging output. For example, you can add the runtimeType and the relevant object in the log that can help you in debugging the code.

log(
    'test log',
    name: runtimeType.toString(),
    error: myObj,
);

** btw I tried log with ANSI code log("\x1B[31m HelloDebug \x1B[0m");, it is working as expected.

enter image description here

luke77
  • 2,255
  • 2
  • 18
  • 30
  • 1
    Thanks for your answer.Actually `debugPrint` prints the log in release mode also. To avoid you need to override the debugPrint function like this https://stackoverflow.com/a/74936653/9248098 – Gowtham K K Jan 08 '23 at 09:01
  • You're welcome. Yes, debugPrint just apply a throttles but not to remove the log in release mode – luke77 Jan 08 '23 at 09:09
  • That ANSI character right. In VS code , for both `debugPrint` and `log` its working fine. But in Android Studio, for `debugPrint` its working fine. But for `log` its not working. – Gowtham K K Jan 08 '23 at 09:12
  • I guess it is not well supported in Android Studio based on some of the answers and comments from this post https://stackoverflow.com/questions/54018071/how-to-call-print-with-colorful-text-to-android-studio-console-in-flutter – luke77 Jan 08 '23 at 11:15
  • Yes I checked that. In newer versions , it has support for ansi codes. But its not working for `log()` but works for print. – Gowtham K K Jan 09 '23 at 06:15