如何从Dart语言登录到浏览器控制台,如JavaScript中的console.log?
发布于 2012-01-22 12:53:58
简单:
print('This will be logged to the console in the browser.');Dart的所有实现(浏览器、VM等)都始终提供基本的顶级print功能。因为Dart有字符串插值,所以很容易用它来打印有用的东西:
var a = 123;
var b = Point(2, 3);
print('a is $a, b is ${b.x}, ${b.y}');发布于 2012-01-22 20:48:50
此外,dart:html还允许使用window.console对象。
import 'dart:html';
void main() {
window.console.debug("debug message");
window.console.info("info message");
window.console.error("error message");
}发布于 2018-10-28 06:23:30
很简单!只需导入日志包:
import 'package:logging/logging.dart';创建记录器对象:
final _logger = Logger('YourClassName');然后在你的代码中,当你需要记录一些东西的时候:
_logger.info('Request received!');如果你捕捉到一个异常,你可以记录它和堆栈跟踪。
_logger.severe('Oops, an error occurred', err, stacktrace);https://stackoverflow.com/questions/8953930
复制相似问题