返回顶部
首页 > 资讯 > 移动开发 >flutter开发实战-日志logger写入文件及print
  • 657
分享到

flutter开发实战-日志logger写入文件及print

flutterLogger日志 2023-09-14 06:09:47 657人浏览 八月长安
摘要

Flutter开发实战-日志logger写入文件及print 在开发中,需要日志logger写入文件,方便日后查看出现的问题。这里记录之前的实现方案。 使用的日志插件是logger 一、引入日志插件

Flutter开发实战-日志logger写入文件及print

在开发中,需要日志logger写入文件,方便日后查看出现的问题。这里记录之前的实现方案。
使用的日志插件是logger
在这里插入图片描述

一、引入日志插件

在工程中pubspec.yaml引入logger

logger: ^1.4.0

二、代码实现

使用比较简单,只需创建一个Logger实例并开始日志记录:

var logger = Logger();logger.d("Logger is working!");

也可以传递其他对象,如List、Map或Set,而不是字符串消息。

2.1 日志logger_manager

  • 使用logger时候,配置logger
FileOutput fileOutPut = FileOutput();    ConsoleOutput consoleOutput = ConsoleOutput();    List<LoGoutput> multioutput = [fileOutPut, consoleOutput];    logger = Logger(      filter: DevelopmentFilter(),      // Use the default LogFilter (-> only log in debug mode)      // printer: SimplePrinter(      //   colors: true,      //   printTime: true,      // ),      printer: HybridPrinter(        PrettyPrinter(          noBoxingByDefault: false,          methodCount: 2,          // number of method calls to be displayed          errORMethodCount: 8,          // number of method calls if stacktrace is provided          lineLength: 120,          // width of the output          colors: true,          // Colorful log messages          printEmojis: false,          // Print an emoji for each log message          printTime: true, // Should each log print contain a timestamp        ),        debug: SimplePrinter(),      ),
  • 写入文件的FileOutPut,其中用到了iOSink

IOSink可以方便将字节和文本输出,IOSink将字节的StreamSsink与StringSink组合,并且允许容易地输出字节和文本。

/// Writes the log output to a file.class FileOutput extends LogOutput {  final bool overrideExisting;  final Encoding encoding;  IOSink? _sink;  File? file;  String? _currentDate;  FileOutput({    this.overrideExisting = false,    this.encoding = utf8,  });  Future<void> getDirectoryForLogRecord() async {    String currentDate = getCurrentDay();    if (currentDate != _currentDate) {      final String fileDir = await createDirectory();      file = File('${fileDir}/${currentDate}.log');      _sink = file!.openWrite(        mode: overrideExisting ? FileMode.writeOnly : FileMode.writeOnlyAppend,        encoding: encoding,      );      _currentDate = currentDate;    }  }  String getCurrentDay() {    String currentDate =        DateUtil.formatDate(DateTime.now(), format: "yyyyMMdd");    return currentDate;  }    void init() {    directoryLogRecord(onCallback: () {});  }  void directoryLogRecord({required Function onCallback}) {    getDirectoryForLogRecord().whenComplete(() {      onCallback();    });  }    void output(OutputEvent event) {    directoryLogRecord(onCallback: () {      if (_sink != null) {        if (Level.info == event.level ||            Level.warning == event.level ||            Level.error == event.level) {          _sink?.writeAll(event.lines, '\n');        }      }    });  }    void destroy() async {    await _sink?.flush();    await _sink?.close();  }}
  • 实现使用logger_manager
Future<String> createDirectory() async {  final Directory directory = await getApplicationDocumentsDirectory();  var file = Directory(directory.path+"/"+"lyd");  try {    bool exist = await file.exists();    if (exist == false) {      await file.create();    }  } catch(e) {    print("createDirectory error");  }  return file.path;}class LoggerManager {  //私有构造函数  LoggerManager._internal() {    deleteLogsOfBefore7Day();    initLogger();  }  //保存单例  static LoggerManager _singleton = LoggerManager._internal();  //工厂构造函数  factory LoggerManager() => _singleton;  late Logger logger;  // log初始化设置  Future<void> initLogger() async {    FileOutput fileOutPut = FileOutput();    ConsoleOutput consoleOutput = ConsoleOutput();    List<LogOutput> multiOutput = [fileOutPut, consoleOutput];    logger = Logger(      filter: DevelopmentFilter(),      // Use the default LogFilter (-> only log in debug mode)      // printer: SimplePrinter(      //   colors: true,      //   printTime: true,      // ),      printer: HybridPrinter(        PrettyPrinter(          noBoxingByDefault: false,          methodCount: 2,          // number of method calls to be displayed          errorMethodCount: 8,          // number of method calls if stacktrace is provided          lineLength: 120,          // width of the output          colors: true,          // Colorful log messages          printEmojis: false,          // Print an emoji for each log message          printTime: true, // Should each log print contain a timestamp        ),        debug: SimplePrinter(),      ),      // printer: PrefixPrinter(PrettyPrinter(      //   noBoxingByDefault: true,      //   methodCount: 2,      //   // number of method calls to be displayed      //   errorMethodCount: 8,      //   // number of method calls if stacktrace is provided      //   lineLength: 120,      //   // width of the output      //   colors: true,      //   // Colorful log messages      //   printEmojis: false,      //   // Print an emoji for each log message      //   printTime: true, // Should each log print contain a timestamp      // )),      // printer: PrettyPrinter(      //   noBoxingByDefault: true,      //   methodCount: 2,      //   // number of method calls to be displayed      //   errorMethodCount: 8,      //   // number of method calls if stacktrace is provided      //   lineLength: 120,      //   // width of the output      //   colors: true,      //   // Colorful log messages      //   printEmojis: false,      //   // Print an emoji for each log message      //   printTime: true, // Should each log print contain a timestamp      // ),      // Use the PrettyPrinter to format and print log      output: MultiOutput(        multiOutput,      ), // Use the default LogOutput (-> send everything to console)    );  }  // Debug  void debug(String message) {    logger.d(message);  }  // verbose  void verbose(String message) {    logger.v(message);  }  // info  void info(String message) {    logger.i(message);  }  // warning  void warning(String message) {    logger.w(message);  }  // error  void error(String message) {    logger.e(message);  }  // 每次启动只保留7天内的日志,删除7天前的日志  Future<void> deleteLogsOfBefore7Day() async {    final String fileDir = await createDirectory();    // 获取目录的所有文件    var dir = Directory(fileDir);    Stream<FileSystemEntity> file = dir.list();    await for (FileSystemEntity x in file) {      // 获取文件的的名称      List<String> paths = x.path.split('/');      if (paths.isNotEmpty) {        String logName = paths.last.replaceAll('.log', '');        final logDate = DateUtil.getDateTime(logName);        final currentDate = DateTime.now();        //比较相差的天数        if (logDate != null) {          final difference = currentDate.difference(logDate!).inDays;          print("deleteLogsOfBefore7Day logDate:${logDate}, currentDate:${currentDate}, difference:${difference}");          if (difference > 7) {            var file = File(x.path);            // 删除文件            file.delete();          }        }      }    }  }}

2.2 在main.dart初始化logger

// 配置logger  await LoggerManager().initLogger();

2.3 使用LoggerManager

logger的level定义

enum Level {  verbose,  debug,  info,  warning,  error,  wtf,  nothing,}

可以在需要的地方使用LoggerManager

// verbose:LoggerManager().verbose("App started at main.dart");// debug:LoggerManager().debug("App started at main.dart");// info:LoggerManager().info("App started at main.dart");// warning:LoggerManager().warning("App started at main.dart");// error:LoggerManager().error("App started at main.dart");// wtf:LoggerManager().wtf("App started at main.dart");// nothing:LoggerManager().nothing("App started at main.dart");

三、小结

flutter开发实战-日志logger写入文件及print,使用的是logger进行实现,输出日志logger的不同level:verbose、debug、info、warning、error、wtf、nothing。

学习记录,每天不停进步。

来源地址:https://blog.csdn.net/gloryFlow/article/details/131654007

--结束END--

本文标题: flutter开发实战-日志logger写入文件及print

本文链接: https://lsjlt.com/news/406788.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作