【发布时间】:2018-10-01 02:47:55
【问题描述】:
我是初学者并使用 IntelliJ IDEA,我想将数据记录到控制台?
我尝试了print() 和printDebug(),但我的数据都没有显示在 Flutter 控制台中。
【问题讨论】:
标签: intellij-idea console dart flutter
我是初学者并使用 IntelliJ IDEA,我想将数据记录到控制台?
我尝试了print() 和printDebug(),但我的数据都没有显示在 Flutter 控制台中。
【问题讨论】:
标签: intellij-idea console dart flutter
如果你在 Flutter Widget 中,你可以使用debugPrint,例如,
import 'package:flutter/foundation.dart';
debugPrint('movieTitle: $movieTitle');
import 'dart:developer';
log('data: $data');
【讨论】:
array [0]、array[1] 或任何运行时分配?
print() 进行打印。我想要做的是仅在调试模式下打印。
log() 上的time 和zone 参数的示例?如何添加它们以使其实际显示?
明确地说,debugPrint 只能在 Flutter 小部件中工作。
【讨论】:
debugPrint 只能在 Flutter 小部件中工作。”。
debugPrint 也适用于模型类(package:scoped_model/scoped_model.dart),它不是 Flutter 小部件
Dart print() 函数输出到系统控制台,您可以使用 Flutter 日志(基本上是 adb logcat 的包装器)查看它。
如果一次输出太多,Android 有时会丢弃一些日志行。为避免这种情况,您可以使用 debugPrint()。
【讨论】:
来自 'dart:developer' 的 log()
它似乎没有像 print() 或 debugPrint() 这样的最大长度限制。
因此,当您想要记录整个 API 响应时,它会很有帮助。
还有助于 dart 开发工具显示格式化的日志记录。
import 'dart:developer'; //(auto import will do this even)
//example for api logging
log("${response?.statusCode} : ${response?.request?.path}",
name: "Response", error: response.data);
【讨论】:
我在代码中使用了 print(),它在调试控制台中打印。
【讨论】:
你可以使用 Logger 包,它很简单
【讨论】:
另外,我为 Flutter Apps 创建了一个 Logger:https://github.com/hiteshsahu/Flutter-Logger
只需复制 AppLog.dart 并添加到您的项目并像这样使用它:
示例:
输入
AppLog.v("-----------------------------");
AppLog.d("I am Debug Log With Default TAG");
AppLog.i("I am Info Log With Default TAG");
AppLog.w("I am Warn Log With Default TAG");
AppLog.e("I am Error Log With Default TAG");
AppLog.wtf("I am Failure Log With Default TAG");
AppLog.v("I am Verbose Log With Default TAG");
//With TAGS
AppLog.v("-----------------------------");
AppLog.d("I am Debug Log With Custom TAG", tag: "Awesome Widget");
AppLog.i("I am Info Log With Custom TAG", tag: "Awesome Widget");
AppLog.w("I am Warn Log With Custom TAG", tag: "Awesome Widget");
AppLog.e("I am Error Log With Custom TAG", tag: "Awesome Widget");
AppLog.wtf("I am Failure Log With Custom TAG", tag: "Awesome Widget");
AppLog.v("I am Verbose Log With Custom TAG", tag: "Awesome Widget");
AppLog.v("-----------------------------");
输出:
Restarted application in 347ms. FlutterApp: ----------------------------- DEBUG|FlutterApp: I am Debug Log With Default TAG INFOⓘ|FlutterApp: I am Info Log With Default TAG WARN⚠️|FlutterApp: I am Warn Log With Default TAG ERROR⚠️|️FlutterApp: I am Error Log With Default TAG WTF¯\_(ツ)_/¯|FlutterApp: I am Failure Log With Default TAG FlutterApp: I am Verbose Log With Default TAG FlutterApp: ----------------------------- DEBUG|Awesome Widget: I am Debug Log With Custom TAG INFOⓘ|Awesome Widget: I am Info Log With Custom TAG WARN⚠️|Awesome Widget: I am Warn Log With Custom TAG ERROR⚠️|️Awesome Widget: I am Error Log With Custom TAG WTF¯\_(ツ)_/¯|Awesome Widget: I am Failure Log With Custom TAG Awesome Widget: I am Verbose Log With Custom TAG FlutterApp: -----------------------------
您还可以为日志级别设置过滤器
AppLog.setLogLevel(log_priority); // log_priority 以下的日志将被隐藏
优先级始终是:
详细
优先级:VERBOSE
示例 隐藏所有信息和调试日志:
AppLog.setLogLevel(AppLog.WARN);
AppLog.v("-----------------------------");
AppLog.d("Debug Log Will not be Visible");
AppLog.i("Info Log Will not be Visible");
AppLog.w("Warn Log Will be Visible");
AppLog.e("Error Log Will be Visible");
AppLog.wtf("Failure Log Will be Visible");
AppLog.v("Verbose Log Will not be Visible");
AppLog.v("-----------------------------");
输出
WARN⚠️|FlutterApp: Warn Log Will be Visible ERROR⚠️|️FlutterApp: Error Log Will be Visible WTF¯\_(ツ)_/¯|FlutterApp: Failure Log Will be Visible
请随意使用我的记录器,或者如果您有一些改进的想法,请创建 PR 或让我知道我会改进它。
【讨论】: