12

I use log from import 'dart:developer'; but it seems that it does not print anything when I use from unit test.

What should I use for logging in both test and dev environments but not in production?

Thanks.

mmdc
  • 1,677
  • 3
  • 20
  • 32

2 Answers2

1

dart:developer only works in debug environments where the Dart vm service is available. As such, launching the unit tests with the debugger attached should work fine. It programmatically interacts with the Dart VM and debugger, so if those aren't attached it won't work properly.

However, logging can be very helpful for production environments as well, so I’d recommend migrating to a different package for a robust logging service across environments. dart:developer is not designed as a logger.

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
  • That is not correct, `dart:developer` does not only work in "debug environments" and its documentation doesn't say anything about it only working in "debug environments": https://api.flutter.dev/flutter/dart-developer/dart-developer-library.html – Maks Nov 04 '21 at 05:02
  • Nope. You are not correct. "dart: developer - Interact with developer tools such as the debugger and inspector." If you don't have a debugger it won't work. Have you actually tested out my answer on unit tests or are you just sabotaging? If you actually tried running unit tests without a debugger attached you can see that `dart:developer` needs a debugger attached. Let me know once you've actually tried it. – Pranav Kasetti Nov 04 '21 at 10:54
  • Well you are right & wrong. `dart:developer` is primarily intended to work with debugger /inspector, BUT this question is **specifically** about using `log()` & specifically about seeing its *output* when running unit tests. For that you do not *need* a debugger, VSC Dartcode & AS plugin both have support for displaying `log()` output, but actually testing it in VSC, seems to be a bug now with its support: https://github.com/Dart-Code/Dart-Code/issues/3653. I edited your answer to clarify the need for vm service & reversed the downvote & updated my answer. – Maks Nov 05 '21 at 00:34
  • Ideally, the answers on SO should work for the language and framework independent of the IDE, plugins or context-specific tools. – Pranav Kasetti Nov 07 '21 at 04:48
0

Messages sent through dart:developer log() will be handled via the Dart VM's native code implementation, note the external function definition in the src code. Tools such as the Flutter Devtools are then free to subscribe to receiving and displaying these messages.

In Flutter apps, the logging will be sent to usual app console output, however if you are using this for a Dart command line application or want output when running unit tests for Flutter apps, you should be able to see that via IDE's, in theory both the VSCode and IntelliJ Dart/Flutter plugins have explicit support added to them to display the output for log(), though currently in the case of DartCode there maybe a bug preventing that.

In theory you can also add support using the Dart vm_service package to read the log messages and send the to stdout/stderr yourself when running unit tests, but in this case it may just be easier to switch to using the logging package published by the Dart team and then you'll likely want to enable explicitly send the output to stdout and/or stderr in the Logger's listen() callback:

// pass true to enable output to stderr
Logger.root.onRecord.listen((LogRecord rec) {
      final mesg = '${rec.level.name}: ${rec.time}: ${rec.message}';
      if (true == useStd) {
        if (rec.level >= Level.SEVERE) {
          stderr.writeln('$mesg\n${rec.error}\n${rec.stackTrace}');
        } else {
          stdout.writeln(mesg);
        }
      } else {
        developer.log(mesg, level: rec.level.value, error: rec.error);
      }
    });

See the implementation in my package for more details.

Maks
  • 7,562
  • 6
  • 43
  • 65
  • Your linked answer refers to a `Logger` from `package:logging`, not `dart:developer` as this question asks. – Christopher Moore Oct 23 '21 at 18:47
  • @ChristopherMoore thanks for reminding me about this answer. I've now updated it to point to my package which does demonstrate usage of `dart:developer` per the original question. – Maks Oct 25 '21 at 00:57