94

console.logJavaScript のように、Dart 言語からブラウザー コンソールにログインするにはどうすればよいですか?

4

5 に答える 5

135

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}');
于 2012-01-22T04:53:58.070 に答える
59

また、オブジェクトdart:htmlの使用を許可しwindow.consoleます。

import 'dart:html';

void main() {
  window.console.debug("debug message");
  window.console.info("info message");
  window.console.error("error message");
}
于 2012-01-22T12:48:50.397 に答える
11

それは簡単です!logging パッケージをインポートするだけです:

import 'package:logging/logging.dart';

ロガー オブジェクトを作成します。

final _logger = Logger('YourClassName');

次に、何かをログに記録する必要がある場合は、コードで次のようにします。

_logger.info('Request received!');

例外をキャッチすると、それとスタックトレースもログに記録できます。

_logger.severe('Oops, an error occurred', err, stacktrace);

Logging パッケージのドキュメント: https://github.com/dart-lang/logging

于 2018-10-27T22:23:30.447 に答える