Flutter - 如何登录到移动设备而不是控制台上的文件

use*_*142 9 logging flutter

文档说

var logger = Logger(
  filter: null, // Use the default LogFilter (-> only log in debug mode)
  printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log
  output: null, // Use the default LogOutput (-> send everything to console)
);
Run Code Online (Sandbox Code Playgroud)

我对输出的参数应该是什么以使其记录到文件感到困惑

file(../lib/file.txt)什么?

Tej*_*oid 9

@用户2962142,

您必须使用扩展 LogOutput 创建类 FileOutput 并传入 Logger() 的输出参数,如下所示

_logger = Logger(打印机: PrettyPrinter(), 输出: FileOutput());

其中 FileOutput 类负责在文件中写入漂亮的日志,如下所示

class FileOutput extends LogOutput {
  FileOutput();

  File file;

  @override
  void init() {
    super.init();
    file = new File(filePath);
  }

  @override
  void output(OutputEvent event) async {
    if (file != null) {
      for (var line in event.lines) {
        await file.writeAsString("${line.toString()}\n",
            mode: FileMode.writeOnlyAppend);
      }
    } else {
      for (var line in event.lines) {
        print(line);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 2

尝试 f_logs 插件:https ://pub.dev/packages/f_logs

这将导出一个 .txt 文件