Running `.dart` files does not show logging messages

Viewed 25

I just cannot figure out how to show logging messages when running dart files from terminal.

Example example.dart:

import 'dart:developer';

void main() {
  print('print');
  log('log');
}
Expected output:

print

log

Actual output:

print

I tried calling dart example.dart, dart run --all example.dart, dart run --verbosity=all example.dart, and different values instead of all (info, ...).

But non of these produced any helpful error messages let alone the out print I expect.

1 Answers

APIs from dart:developer (such as log) are intended to interact with debugging tools:

Interact with developer tools such as the debugger and inspector.

It's not explicit from the documentation for log, but I'd expect it to send a log message only to an attached debugger, not to the console.

If you want logging output that is independent of a debugger, use package:logging and add a listener that calls print, as shown by the example.

Related