Andreas Köberle
Andreas Köberle

Reputation: 110892

Console.log in Dart Language

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

Upvotes: 109

Views: 71749

Answers (6)

Ivan Tsaryk
Ivan Tsaryk

Reputation: 1693

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.

Upvotes: 6

Lars Lehmann
Lars Lehmann

Reputation: 21

When you use only Dart without Flutter, then this is a good and easy solution:

void log(var logstr) {
  stdout.writeln("-> " + logstr.toString());
}

Upvotes: 0

munificent
munificent

Reputation: 12364

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}');

Upvotes: 159

Simple: print("hello word"); or debugPrint(" hello word);

Upvotes: 2

Mark Madej
Mark Madej

Reputation: 1912

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

Upvotes: 10

Chris Buckett
Chris Buckett

Reputation: 14388

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");
}

Upvotes: 65

Related Questions