How can I log into the browser console, like console.log
in JavaScript, from the Dart language?

- 106,652
- 57
- 273
- 297
-
You can also try packages like logging or scribe https://pub.dartlang.org/packages/logging https://pub.dartlang.org/packages/scribe – Hadrien Lejard Oct 31 '16 at 07:18
6 Answers
Simple:
print('This will be logged to the console in the browser.');
A basic top-level print
function is always available in all implementations of Dart (browser, VM, etc.). Because Dart has string interpolation, it's easy to use that to print useful stuff too:
var a = 123;
var b = Point(2, 3);
print('a is $a, b is ${b.x}, ${b.y}');

- 11,946
- 2
- 38
- 55
Also, dart:html
allows use of window.console
object.
import 'dart:html';
void main() {
window.console.debug("debug message");
window.console.info("info message");
window.console.error("error message");
}

- 15,395
- 32
- 113
- 196

- 13,738
- 5
- 39
- 46
-
This seems work in firefox, at least i tested window.console.debug('') – Gökhan Barış Aker Nov 23 '12 at 09:54
-
2This answer is much better, because you can traverse object graphs in the console just as you can do it with any JS library. Does work in current FF. – Akos Lukacs Dec 05 '17 at 08:31
-
in combination with Flutter you will get for the import: _Avoid using web-only libraries outside Flutter web plugin packages_ – Wizard of Kneup Dec 25 '21 at 17:31
It’s easy! Just import the logging package:
import 'package:logging/logging.dart';
Create a logger object:
final _logger = Logger('YourClassName');
Then in your code when you need to log something:
_logger.info('Request received!');
If you catch an exception you can log it and the stacktrace as well.
_logger.severe('Oops, an error occurred', err, stacktrace);
Logging package documentation : https://github.com/dart-lang/logging

- 11,553
- 8
- 64
- 88

- 1,752
- 1
- 14
- 19
-
3this is a dependency, needs to be added to pubspec.yml dependencies: logging: ^0.11.4 – ir2pid Feb 06 '20 at 13:08
-
1You also have to add some code to make the Logger do something useful. If you only have the code above, the log messages will be sent into the void. To direct log messages to the console, for instance, you can use code like this: `// Configure logging to output to console: Logger.root.level = Level.ALL; Logger.root.onRecord.listen((record) { print('${record.level.name}: ${record.time}: ${record.message}'); });` – Pi Da Aug 05 '20 at 18:59
-
This does not answer the question of "writing to the console". Doing just what you said doest nothing unless you register a logger that writes to the console... And then you're back at square one of trying to find a way to write to the console. – Stephan Leclercq Dec 06 '20 at 07:42
You can use Dart's built-in log()
function
import 'dart:developer';
log('data: $data');
You can also use print()
, but this is not a good practice because it can slow down your program in a production environment. debugPrint
, log
and other methods will prevent this.

- 1,193
- 2
- 8
- 10
Simple:
print("hello word");
or
debugPrint(" hello word);

- 63
- 7
-
1debugPrint requires import 'package:flutter/foundation.dart'; – Stephan Leclercq Dec 06 '20 at 07:54
When you use only Dart without Flutter, then this is a good and easy solution:
void log(var logstr) {
stdout.writeln("-> " + logstr.toString());
}