马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
日记
利用print方法时,会提示
添加依赖
下载依赖
利用
打印
- import 'package:logger/logger.dart';
- var logger = Logger();
- logger.d("debug");
- logger.e("error");
- logger.i("info");
- logger.f("fatal");
- logger.w("warning");
- logger.t("trace");
复制代码
设置打印级别
- /// The current logging level of the app.
- /// All logs with levels below this level will be omitted.
- /// 设置打印级别,低于level的不会被打印
- Logger.level;
复制代码- enum Level {
- all(0),
- @Deprecated('[verbose] is being deprecated in favor of [trace].')
- verbose(999),
- trace(1000),
- debug(2000),
- info(3000),
- warning(4000),
- error(5000),
- @Deprecated('[wtf] is being deprecated in favor of [fatal].')
- wtf(5999),
- fatal(6000),
- @Deprecated('[nothing] is being deprecated in favor of [off].')
- nothing(9999),
- off(10000),
- ;
- final int value;
- const Level(this.value);
- }
复制代码 打印到文件
- /// 使用 getApplicationDocumentsDirectory 需要
- /// 添加 path_provider
- /// flutter pub add path_provider
- /// flutter pub ge
- /// - `NSDocumentDirectory` on iOS and macOS.
- /// - The Flutter engine's `PathUtils.getDataDirectory` API on Android.
- Directory documentDir = await getApplicationDocumentsDirectory();
- var logger = Logger(
- output: FileOutput(file: File("${documentDir.path}/log.txt")),
- );
复制代码 打印到控制台+文件
- Directory documentDir = await getApplicationDocumentsDirectory();
- FileOutput fileOutPut = FileOutput(
- file: File("${documentDir.path}/log.txt"),
- );
- ConsoleOutput consoleOutput = ConsoleOutput();
- List<LogOutput> multiOutput = [fileOutPut, consoleOutput];
- var logger = Logger(
- output: MultiOutput(multiOutput),
- );
复制代码
打印时间
Logger构造函数有个printer参数可以指定输出格式
- var logger = Logger(
- filter: null, // Use the default LogFilter (-> only log in debug mode)
- printer: PrettyPrinter(dateTimeFormat: DateTimeFormat.dateAndTime),
- output: MultiOutput(multiOutput),
- );
复制代码
打印过滤器
默认的过滤器(DevelopmentFilter)在debug模式下可以打印所有level >= Logger.level的日记,在Release模式下所有日记被忽略。
好比设置Release模式下只打印warning级别以上(含warning)的日记
- class MyFilter extends LogFilter {
- @override
- bool shouldLog(LogEvent event) {
- if (event.level.value < Logger.level.value) {
- return false;
- }
- return true;
- }
- }
复制代码- ...
- const bool inProduction = bool.fromEnvironment("dart.vm.product");
- if (inProduction) {
- Logger.level = Level.warning;
- }
- ...
- ConsoleOutput consoleOutput = ConsoleOutput();
- List<LogOutput> multiOutput = [fileOutPut, consoleOutput];
- var logger = Logger(
- filter:
- MyFilter(), // Use the default LogFilter (-> only log in debug mode)
- printer: PrettyPrinter(dateTimeFormat: DateTimeFormat.dateAndTime),
- output: MultiOutput(multiOutput),
- );
- logger.d("debug");
- logger.e("error");
- logger.i("info");
- logger.f("fatal");
- logger.w("warning");
- logger.t("trace");
复制代码
参考
- logger
- Flutter-文件体系目录和文件读写
- Flutter: How to show log output in console and automatically store it?
- Flutter中怎样判定当前运行模式是Debug照旧Release
|