5

This is a duplicate of another question. Actually I have exactly the same question and providing Logger.root.onRecord.listen didn't help. Also this answer reports developer.log() can be used to log data to flutter console.

Source code:

import 'dart:io';
import 'dart:developer' as developer;

import 'package:logging/logging.dart';

void main(List<String> arguments) {
  stderr.writeln('Hello from stderr');
  stdout.writeln('Hello from stdout');
  print('Hello from print');

  print('Logger level ${Logger.root.level}');
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((r) => print(r));
  var log = Logger('MyLogger');
  for (var level in Level.LEVELS) {
    print('Level: $level, isLoggable: ${log.isLoggable(level)}');
    log.log(level, 'Log message with level $level');
  }

  developer.log('Hello from developer.log');
}

That produces the following output:

/opt/java/jdk/flutter/bin/cache/dart-sdk/bin/dart --enable-asserts /home/m_pashka/Projects/tests/flutter/dart-test1/bin/dart_test3.dart
Hello from stdout
Hello from print
Logger level INFO
Level: ALL, isLoggable: true
[ALL] MyLogger: Log message with level ALL
Level: FINEST, isLoggable: true
[FINEST] MyLogger: Log message with level FINEST
Level: FINER, isLoggable: true
[FINER] MyLogger: Log message with level FINER
Level: FINE, isLoggable: true
[FINE] MyLogger: Log message with level FINE
Level: CONFIG, isLoggable: true
[CONFIG] MyLogger: Log message with level CONFIG
Level: INFO, isLoggable: true
[INFO] MyLogger: Log message with level INFO
Level: WARNING, isLoggable: true
[WARNING] MyLogger: Log message with level WARNING
Level: SEVERE, isLoggable: true
[SEVERE] MyLogger: Log message with level SEVERE
Level: SHOUT, isLoggable: true
[SHOUT] MyLogger: Log message with level SHOUT
Level: OFF, isLoggable: true
[OFF] MyLogger: Log message with level OFF
Hello from stderr

Process finished with exit code 0

No Hello from developer.log line there. I also tried different dart versions: 2.0.0, 2.13.4, 2.14.4.

I used dart command line to run example: <dart_sdk_path>/dart-sdk/bin/dart --enable-asserts <project_path>/<file>.dart

Also tried this snippet in Dart Pad:

import 'dart:developer' as developer;

void main() {
  developer.log('Hello from developer.log');
  developer.log('log me', name: 'my.app.category');
  print('Hello from print');
}

Output is:

Hello from print
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Pavel Moukhataev
  • 149
  • 1
  • 10
  • 1
    The `log` function from `dart:developer` emits log *events*. For debugging and DevTools. It's not supposed to put anything on stdout so it's working as expected. – Christopher Moore Oct 23 '21 at 15:32
  • The person answering the linked question is very much wrong. – Christopher Moore Oct 23 '21 at 18:50
  • Log [function description](https://api.flutter.dev/flutter/dart-developer/log.html) reports developer.log() emits log event without much description on how that can be used. [Debugging](https://flutter.dev/docs/testing/code-debugging#logging) reports developer.log() can be used to print debug messages in the same way as stderr.writeln(). So I still not sure what is expected behavior. – Pavel Moukhataev Oct 23 '21 at 20:10
  • As I briefly mentioned in my previous comment, these logs are shown in DevTools. A debugger may also be able to take advantage of these logs. https://flutter.dev/docs/development/tools/devtools/logging – Christopher Moore Oct 23 '21 at 20:12
  • The expected behavior is to emit a log event. The link you shared does not say `log` can be used to output on stdout or stderr. Quote from your link: "You have two options for logging for your application. The first is to use stdout and stderr. [...] The other option for application logging is to use the dart:developer log() function." – Christopher Moore Oct 23 '21 at 20:14

2 Answers2

5

As it was mentioned earlier by Christopher Moore developer.log() doesn't print anything to the dart console. Instead it emits events that can be observed by the Logging View of DevTools. You can use Install and run DevTools from the command line for instructions on how to install and launch DevTools and Start the target app section of Dart DevTools for command to start the Dart command-line app to be observed in dev tools.

So it is something like

  • # dart pub global activate devtools to install devtools
  • # dart pub global run devtools to start devtools (can be omited)
  • # dart run --pause-isolates-on-start --observe main.dart to start main.dart with devtools connected. On this step you can either use short URL and put into DevTools web UI or just use long link to open DevTools in your browser.
  • open "Logging" tab at the top of DevTools web page, resume application execution and observe logs.

Still not sure about correctness of Debugging Flutter apps programmatically # Logging page documentation. It mentions

Note: You can view logs in DevTools’ Logging view or in your system console. This sections shows how to set up your logging statements.

But it doesn't mention that stderr.writeln() is visible in Logging view but developer.log() is not visible in console output. Also it is unclear why is that Logging view not visible in Intellij Idea/Android studio.

Pavel Moukhataev
  • 149
  • 1
  • 10
1

**There is an issue with a log for printing map data you need to try this package **

logger

using this you can easily print different types of responses under different tags listed below

logger.v("Verbose log");

logger.d("Debug log");

logger.i("Info log");

logger.w("Warning log");

logger.e("Error log");

logger.wtf("What a terrible failure log");
Aqib shahzad
  • 491
  • 1
  • 4
  • 11