Flutter - 集成三方库:日记(logger)

[复制链接]
发表于 2025-8-22 01:26:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
日记

利用print方法时,会提示

添加依赖

  1. $ flutter pub add logger
复制代码
下载依赖

  1. $ flutter pub get
复制代码
利用

打印

  1. import 'package:logger/logger.dart';
  2. var logger = Logger();
  3. logger.d("debug");
  4. logger.e("error");
  5. logger.i("info");
  6. logger.f("fatal");
  7. logger.w("warning");
  8. logger.t("trace");
复制代码

设置打印级别

  1. /// The current logging level of the app.
  2. /// All logs with levels below this level will be omitted.
  3. /// 设置打印级别,低于level的不会被打印
  4. Logger.level;
复制代码
  1. enum Level {
  2.   all(0),
  3.   @Deprecated('[verbose] is being deprecated in favor of [trace].')
  4.   verbose(999),
  5.   trace(1000),
  6.   debug(2000),
  7.   info(3000),
  8.   warning(4000),
  9.   error(5000),
  10.   @Deprecated('[wtf] is being deprecated in favor of [fatal].')
  11.   wtf(5999),
  12.   fatal(6000),
  13.   @Deprecated('[nothing] is being deprecated in favor of [off].')
  14.   nothing(9999),
  15.   off(10000),
  16.   ;
  17.   final int value;
  18.   const Level(this.value);
  19. }
复制代码
打印到文件

  1. /// 使用 getApplicationDocumentsDirectory 需要
  2. /// 添加 path_provider
  3. /// flutter pub add path_provider
  4. /// flutter pub ge
  5. /// - `NSDocumentDirectory` on iOS and macOS.
  6. /// - The Flutter engine's `PathUtils.getDataDirectory` API on Android.
  7. Directory documentDir = await getApplicationDocumentsDirectory();
  8. var logger = Logger(
  9.   output: FileOutput(file: File("${documentDir.path}/log.txt")),
  10. );
复制代码
打印到控制台+文件

  1. Directory documentDir = await getApplicationDocumentsDirectory();
  2. FileOutput fileOutPut = FileOutput(
  3.   file: File("${documentDir.path}/log.txt"),
  4. );
  5. ConsoleOutput consoleOutput = ConsoleOutput();
  6. List<LogOutput> multiOutput = [fileOutPut, consoleOutput];
  7. var logger = Logger(
  8. output: MultiOutput(multiOutput),
  9. );
复制代码

打印时间

Logger构造函数有个printer参数可以指定输出格式
  1. var logger = Logger(
  2.     filter: null, // Use the default LogFilter (-> only log in debug mode)
  3.     printer: PrettyPrinter(dateTimeFormat: DateTimeFormat.dateAndTime),
  4.     output: MultiOutput(multiOutput),
  5. );
复制代码

打印过滤器

默认的过滤器(DevelopmentFilter)在debug模式下可以打印所有level >= Logger.level的日记,在Release模式下所有日记被忽略。
好比设置Release模式下只打印warning级别以上(含warning)的日记
  1. class MyFilter extends LogFilter {
  2.   @override
  3.   bool shouldLog(LogEvent event) {
  4.     if (event.level.value < Logger.level.value) {
  5.       return false;
  6.     }
  7.     return true;
  8.   }
  9. }
复制代码
  1. ...
  2. const bool inProduction = bool.fromEnvironment("dart.vm.product");
  3. if (inProduction) {
  4.   Logger.level = Level.warning;
  5. }
  6. ...
  7. ConsoleOutput consoleOutput = ConsoleOutput();
  8.     List<LogOutput> multiOutput = [fileOutPut, consoleOutput];
  9.     var logger = Logger(
  10.       filter:
  11.           MyFilter(), // Use the default LogFilter (-> only log in debug mode)
  12.       printer: PrettyPrinter(dateTimeFormat: DateTimeFormat.dateAndTime),
  13.       output: MultiOutput(multiOutput),
  14. );
  15. logger.d("debug");
  16. logger.e("error");
  17. logger.i("info");
  18. logger.f("fatal");
  19. logger.w("warning");
  20. logger.t("trace");
复制代码

参考


  • logger
  • Flutter-文件体系目录和文件读写
  • Flutter: How to show log output in console and automatically store it?
  • Flutter中怎样判定当前运行模式是Debug照旧Release
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表