Flutter日志打印插件ansi_logger的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter日志打印插件ansi_logger的使用

ansi_logger 是一个用于在控制台中以彩色输出格式打印日志的Flutter插件。它提供了多种日志记录方式,包括简单的文本日志、带边框的日志、JSON格式日志以及HTTP请求和响应的日志。

如何使用

安装插件

首先,在您的 pubspec.yaml 文件中添加 ansi_logger 依赖:

dependencies:
  flutter:
    sdk: flutter
  ansi_logger: ^1.0.0 # 请根据最新的版本号进行替换
  dio: ^4.0.0 # 如果需要处理Dio请求,请添加此依赖

然后运行 flutter pub get 来安装这些依赖。

示例代码

以下是一个完整的示例项目,展示了如何使用 ansi_logger 插件来打印不同类型的日志。

import 'dart:convert';
import 'dart:math';

import 'package:ansi_logger/ansi_logger.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              child: Text("Simple log"),
              onPressed: () {
                SimpleAnsiLogger.log(AnsiColors.red, "حبيبي يارسول الله");
              },
            ),
            ElevatedButton(
              child: Text("Box log"),
              onPressed: () {
                AnsiLogger logger = AnsiLogger();
                logger.logBox(
                  "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).",
                );
              },
            ),
            ElevatedButton(
              child: Text("Advanced log"),
              onPressed: () {
                AnsiLogger logger = AnsiLogger();
                logger.logBoxStart();
                logger.logString("إلا رسول الله صلي الله عليه وسلم");
                logger.logSpliter();
                logger.logString("إلا رسول الله صلي الله عليه وسلم");
                logger.logSolidLine();
                logger.logString(
                  "إلا رسول الله صلي الله عليه وسلم",
                );
                logger.logDoubleLine();
                logger.logString(
                  "إلا رسول الله صلي الله عليه وسلم",
                );
                logger.logBoxEnd();
              },
            ),
            ElevatedButton(
              child: Text("Json log"),
              onPressed: () {
                AnsiLogger logger = AnsiLogger();
                logger.logBoxStart();
                logger.logJson(
                  {
                    "key_1": "Value 1",
                    "key_t": {},
                    "Key_2": {
                      "Sub_key_1": 7,
                      "Sub_key_2": {
                        "Sub_key": "Sub value",
                      }
                    },
                    "List": [
                      "Value 1",
                      true,
                      {"Key": "Value"},
                      [
                        true,
                        false,
                        1,
                        "Text",
                        DateTime.now(),
                      ]
                    ],
                  },
                );
                logger.logBoxEnd();
              },
            ),
            ElevatedButton(
              child: Text("Log http"),
              onPressed: () {
                AnsiLogger logger = AnsiLogger();
                logger.logHttp(
                  url: "http://site.com",
                  statusMessage: "Done",
                  method: "Get",
                  statusCode: 400,
                  queryParameters: {
                    "key1": 1,
                    "key2": "Value",
                  },
                  requestHeaders: {
                    "key1": 1,
                    "key2": "Value",
                  },
                  responseHeaders: {
                    "key1": 1,
                    "key2": "Value",
                  },
                  files: [
                    IFile("filename1", "key", 76767676),
                    IFile("filename2", "key", 76767676),
                    IFile("filename3", "key", 76767676),
                  ],
                  requestData: {
                    "key1": 1,
                    "key2": "Value",
                  },
                  response: {
                    "key1": 1,
                    "key2": "Value",
                  },
                );
              },
            ),
            ElevatedButton(
              child: Text("Dio"),
              onPressed: () {
                Dio dio = Dio();
                dio.interceptors.add(_Interceptor());

                dio.get("https://jsonplaceholder.typicode.com/todos/1");
              },
            ),
          ],
        ),
      ),
    );
  }
}

class _Interceptor extends Interceptor {
  final logger = AnsiLogger();
  DateTime? _start;
  String? get _duration {
    if (_start == null) return 'without duration ➤ ';
    var duration = DateTime.now().difference(_start!).inMilliseconds;
    return '$duration MS ➤ ';
  }

  dynamic tryGetMap(dynamic data) {
    try {
      if (data is String) {
        return jsonDecode(data);
      } else {
        return data;
      }
    } catch (e) {
      return data;
    }
  }

  [@override](/user/override)
  void onError(DioError err, ErrorInterceptorHandler handler) {
    super.onError(err, handler);
    if (!kDebugMode) return;
    logger.logBox('Got error ${err.requestOptions.uri} at ${DateTime.now()}');
    dynamic requestData;
    List<IFile>? files;
    if (err.requestOptions.data is FormData) {
      var fd = err.requestOptions.data as FormData;
      requestData = {};
      for (var element in fd.fields) {
        requestData[element.key] = element.value;
      }
      files = [];
      for (var k in fd.files) {
        files.add(IFile(k.value.filename ?? '', k.key, k.value.length));
      }
    } else {
      requestData = err.requestOptions.data;
    }

    logger.logHttp(
      url: err.requestOptions.uri.toString(),
      statusMessage: '$_duration${err.response?.statusMessage}',
      method: err.requestOptions.method,
      statusCode: err.response?.statusCode ?? 0,
      queryParameters: err.requestOptions.queryParameters,
      requestHeaders: err.requestOptions.headers,
      responseHeaders: err.response?.headers.map ?? {},
      response: tryGetMap(err.response?.data),
      requestData: requestData,
      files: files,
    );
  }

  static String _formatBytes(int bytes, int decimals) {
    if (bytes <= 0) return '0 B';
    const suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    var i = (log(bytes) / log(1024)).floor();
    return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + ' ' + suffixes[i];
  }

  [@override](/user/override)
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    super.onResponse(response, handler);
    if (!kDebugMode) return;
    logger.logBox('Got response ${response.requestOptions.uri} at ${DateTime.now()}');
    dynamic requestData;
    List<IFile>? files;
    if (response.requestOptions.data is FormData) {
      var fd = response.requestOptions.data as FormData;
      requestData = {};
      for (var element in fd.fields) {
        requestData[element.key] = element.value;
      }
      files = [];
      for (var k in fd.files) {
        files.add(IFile(k.value.filename ?? '', k.key, k.value.length));
      }
    } else {
      requestData = response.requestOptions.data;
    }
    var bytes = _formatBytes(utf8.encode('${response.data}').length, 2);
    logger.logHttp(
      url: response.requestOptions.uri.toString(),
      statusMessage: '$bytes ➤ $_duration${response.statusMessage}',
      method: response.requestOptions.method,
      statusCode: response.statusCode ?? 0,
      queryParameters: response.requestOptions.queryParameters,
      requestHeaders: response.requestOptions.headers,
      responseHeaders: response.headers.map,
      response: tryGetMap(response.data),
      requestData: requestData,
      files: files,
    );
  }
}

更多关于Flutter日志打印插件ansi_logger的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter日志打印插件ansi_logger的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用ansi_logger插件进行日志打印的示例代码。ansi_logger是一个功能强大的日志库,它支持ANSI颜色代码,使日志信息在控制台中更加易于阅读。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加ansi_logger的依赖:

dependencies:
  flutter:
    sdk: flutter
  ansi_logger: ^1.0.1  # 确保版本号是最新的

然后运行flutter pub get来安装依赖。

2. 配置和使用ansi_logger

在你的Flutter项目的Dart文件中,你可以按照以下方式配置和使用ansi_logger

import 'package:flutter/material.dart';
import 'package:ansi_logger/ansi_logger.dart';

void main() {
  // 配置AnsiLogger
  final logger = AnsiLogger(
    printer: PrettyPrinter(
      methodCount: 2,  // 打印调用堆栈的深度
      printEmojis: true,  // 是否打印表情符号
      printTime: true,  // 是否打印时间戳
      colors: true,  // 是否启用ANSI颜色
      printLevel: true,  // 是否打印日志级别
    ),
  );

  // 设置全局日志级别(可选)
  logger.level = LogLevel.verbose;

  // 打印不同级别的日志
  logger.v('This is a verbose log message.');
  logger.d('This is a debug log message.');
  logger.i('This is an info log message.');
  logger.w('This is a warning log message.');
  logger.e('This is an error log message.');

  runApp(MyApp(logger: logger));
}

class MyApp extends StatelessWidget {
  final AnsiLogger logger;

  MyApp({required this.logger});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('AnsiLogger Demo'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              // 在按钮点击时打印日志
              logger.i('Button was pressed!');
            },
            child: Text('Press Me'),
          ),
        ),
      ),
    );
  }
}

3. 运行项目

保存上述代码并运行你的Flutter项目。当你运行应用时,你应该能够在控制台中看到带有颜色和高亮显示的日志信息。

注意事项

  • AnsiLogger的配置参数PrettyPrinter提供了多种选项来自定义日志输出格式,如methodCountprintEmojisprintTimecolorsprintLevel等。
  • 日志级别(LogLevel)可以是verbosedebuginfowarningerror,你可以根据需要设置全局日志级别来控制打印的日志信息。
  • 在实际开发中,你可能会将日志记录器(logger)作为一个依赖注入到你的应用中,以便在整个应用中共享同一个日志记录器实例。

希望这能帮助你在Flutter项目中更好地使用ansi_logger进行日志打印!

回到顶部