Damandroid
Damandroid

Reputation: 974

Print statements in flutter

Print statements are really useful in debugging dart code in flutter. Anyone know the implications of not removing them from the code in the release version?

Upvotes: 6

Views: 2160

Answers (1)

jamesdlin
jamesdlin

Reputation: 89926

As possibly unnecessary I/O, print/debugPrint statements can hurt performance. They also will spam the device log.

Additionally, since such statements can be visible to anything inspecting the device log, they can easily leak information that might be better left private (e.g. usernames and passwords).

I recommend using something like package:logging instead. That way you can provide a callback that uses debugPrint but can adjust your logging level to easily enable or disable messages by severity (and could enable additional logging in debug builds).

The package:logging documentation provides an example, but tweaking it slightly to have different logging levels between debug and release builds:

import 'package:logging' as log;

void main() {
  // Log everything in debug builds.  Log warnings (and up) in release builds.
  log.Logger.root.level = kDebugMode ? log.Level.ALL : log.Level.WARNING;
  log.Logger.root.onRecord.listen((log.LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });

  // ...
}

Upvotes: 7

Related Questions