Rules / Suspicious / avoid-print

avoid-print

lint/suspicious/avoid-print
recommendedflutter
Same as avoid_print · Dart lints

Flags calls to the top-level print function.

print writes to the system console, which is invisible in released builds and can leak diagnostic detail or stall the UI isolate when it runs on a hot path. In application code a surviving print is almost always a debugging statement that was never removed. Route diagnostics through debugPrint, dart:developer's log, or a real logging framework instead. A file that declares its own print (top-level, method, or local function) is left alone, since the call may resolve to that shadow rather than the SDK function.

Invalid

example.dartdart
// Bad: calls to the top-level print function.
void main() {
  print('hello');
  print(42);
  const value = 7;
  print(value);
  print('sum: ${value + 1}');
  final list = [1, 2, 3];
  list.forEach((e) => print(e));
  print('done');
}
Avoid using print in production code; use a logging framework instead.
2void main() {
3 print('hello');
Avoid using print in production code; use a logging framework instead.
3 print('hello');
4 print(42);
Avoid using print in production code; use a logging framework instead.
5 const value = 7;
6 print(value);
Avoid using print in production code; use a logging framework instead.
6 print(value);
7 print('sum: ${value + 1}');
Avoid using print in production code; use a logging framework instead.
8 final list = [1, 2, 3];
9 list.forEach((e) => print(e));
Avoid using print in production code; use a logging framework instead.
9 list.forEach((e) => print(e));
10 print('done');

Valid

example.dartdart
// Good: no calls to the top-level print function.
void main() {
  debugPrint('hello');
  logger.info('message');
  final printer = Printer();
  printer.print('doc');
  stdout.write('line');
  final summary = describe();
  logger.info(summary);
}

void debugPrint(String s) {}

class Printer {
  void print(String s) {}
}

class Logger {
  void info(String s) {}
}

final logger = Logger();
final stdout = StringSink();

class StringSink {
  void write(String s) {}
}

String describe() => 'ok';

How to configure

Set the severity of avoid-print in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-print": "error"
      }
    }
  }
}