From 437901b15becffe549af75488885cbc645852e01 Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Mon, 18 Sep 2023 17:42:06 +0200 Subject: [PATCH 1/7] Improve docs for LogInterceptor --- dio/README-ZH.md | 6 ++++++ dio/README.md | 9 ++++++++- dio/lib/src/interceptors/log.dart | 13 +++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/dio/README-ZH.md b/dio/README-ZH.md index 740aee206..1ef248b4f 100644 --- a/dio/README-ZH.md +++ b/dio/README-ZH.md @@ -475,6 +475,12 @@ print(response.data); // 'fake data' dio.interceptors.add(LogInterceptor(responseBody: false)); // 不输出响应内容体 ``` +When using Flutter, you should use `debugPrint` to print logs: + +```dart +dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); +``` + **注意:** 由于拦截器队列是先进先出,`LogInterceptor` 应当在最后添加至 `Dio` 实例。 **注意:** 日志默认仅在 DEBUG 模式(启用了断言)下打印。 diff --git a/dio/README.md b/dio/README.md index b2ba69d01..3153fab70 100644 --- a/dio/README.md +++ b/dio/README.md @@ -490,7 +490,14 @@ You can apply the `LogInterceptor` to log requests and responses automatically i dio.interceptors.add(LogInterceptor(responseBody: false)); // Do not output responses body. ``` -**Note:** `LogInterceptor` should be the last to add since the interceptors are FIFO. +When using Flutter, you should use `debugPrint` to print logs: + +```dart +dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); +``` + +**Note:** `LogInterceptor` should be the last interceptor added, otherwise modifications by following interceptors +will not be logged. **Note:** Logs will only be printed in the DEBUG mode (when the assertion is enabled). diff --git a/dio/lib/src/interceptors/log.dart b/dio/lib/src/interceptors/log.dart index 04f647077..3de430e77 100644 --- a/dio/lib/src/interceptors/log.dart +++ b/dio/lib/src/interceptors/log.dart @@ -4,9 +4,18 @@ import '../options.dart'; import '../response.dart'; /// [LogInterceptor] is used to print logs during network requests. -/// It's better to add [LogInterceptor] to the tail of the interceptor queue, -/// otherwise the changes made in the interceptor behind A will not be printed out. +/// [LogInterceptor] should be the last interceptor added, +/// otherwise modifications by following interceptors will not be logged. /// This is because the execution of interceptors is in the order of addition. +/// +/// **Note** +/// When used in Flutter, make sure to use `debugPrint` to print logs. +/// +/// ```dart +/// dio.interceptors.add(LogInterceptor( +/// logPrint: (o) => debugPrint(o.toString()), +/// )); +/// ``` class LogInterceptor extends Interceptor { LogInterceptor({ this.request = true, From e2081479914705fae887250e7789adf8142c2d2b Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Wed, 27 Sep 2023 14:14:53 +0200 Subject: [PATCH 2/7] Split docs into Dart/Flutter and add information about dart:developer --- dio/README.md | 24 +++++++++++++++++------- dio/lib/src/interceptors/log.dart | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/dio/README.md b/dio/README.md index 3153fab70..2b0c51384 100644 --- a/dio/README.md +++ b/dio/README.md @@ -484,23 +484,33 @@ For the complete code see [here](../example/lib/queued_interceptor_crsftoken.dar #### LogInterceptor -You can apply the `LogInterceptor` to log requests and responses automatically in the DEBUG mode: +You can apply the `LogInterceptor` to log requests and responses automatically. + +**Note:** `LogInterceptor` should always be the last interceptor added, +otherwise modifications by following interceptors will not be logged. + +#### Dart ```dart dio.interceptors.add(LogInterceptor(responseBody: false)); // Do not output responses body. ``` -When using Flutter, you should use `debugPrint` to print logs: +**Note:** When using the default `logPrint` function, logs will only be printed +in DEBUG mode (when the assertion is enabled). + + +Alternatively `dart:developer`'s log can also be used to log messages. + +#### Flutter + +When using Flutter, Flutters own `debugPrint` function should be used. +This ensures, that debug messages are also available via `flutter logs`. + ```dart dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); ``` -**Note:** `LogInterceptor` should be the last interceptor added, otherwise modifications by following interceptors -will not be logged. - -**Note:** Logs will only be printed in the DEBUG mode (when the assertion is enabled). - #### Custom Interceptor You can customize interceptor by extending the `Interceptor/QueuedInterceptor` class. diff --git a/dio/lib/src/interceptors/log.dart b/dio/lib/src/interceptors/log.dart index 3de430e77..fc2487d4b 100644 --- a/dio/lib/src/interceptors/log.dart +++ b/dio/lib/src/interceptors/log.dart @@ -1,3 +1,5 @@ +import 'dart:developer'; + import '../dio_exception.dart'; import '../dio_mixin.dart'; import '../options.dart'; @@ -10,6 +12,7 @@ import '../response.dart'; /// /// **Note** /// When used in Flutter, make sure to use `debugPrint` to print logs. +/// Alternatively `dart:developer`'s `log` function can also be used. /// /// ```dart /// dio.interceptors.add(LogInterceptor( From 938743158e7fa8dbf5f225c8b46114f050052ca3 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 28 Sep 2023 09:42:38 +0800 Subject: [PATCH 3/7] Update README-ZH.md --- dio/README-ZH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dio/README-ZH.md b/dio/README-ZH.md index 1ef248b4f..e7b2a8bb4 100644 --- a/dio/README-ZH.md +++ b/dio/README-ZH.md @@ -475,7 +475,7 @@ print(response.data); // 'fake data' dio.interceptors.add(LogInterceptor(responseBody: false)); // 不输出响应内容体 ``` -When using Flutter, you should use `debugPrint` to print logs: +在 Flutter 中你应该使用 `debugPrint` 来打印日志: ```dart dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); From 78e13221c307071b712192d3d2ca89f4ca7de9c9 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 28 Sep 2023 09:56:17 +0800 Subject: [PATCH 4/7] Update README-ZH.md --- dio/README-ZH.md | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/dio/README-ZH.md b/dio/README-ZH.md index e7b2a8bb4..73049c83d 100644 --- a/dio/README-ZH.md +++ b/dio/README-ZH.md @@ -469,22 +469,39 @@ print(response.data); // 'fake data' #### 日志拦截器 -我们可以添加 `LogInterceptor` 拦截器来自动打印请求、响应日志: +我们可以添加 `LogInterceptor` 拦截器来自动打印请求和响应等日志: + +**注意:** `LogInterceptor` 应该保持最后一个被添加到拦截器中, +否则在它之后进行处理的拦截器修改的内容将无法体现。 + +#### Dart ```dart dio.interceptors.add(LogInterceptor(responseBody: false)); // 不输出响应内容体 ``` -在 Flutter 中你应该使用 `debugPrint` 来打印日志: +**Note:** When using the default `logPrint` function, logs will only be printed +in DEBUG mode (when the assertion is enabled). + +**注意:** 默认的 `logPrint` 只会在 DEBUG 模式(启用了断言) +的情况下输出日志。 + +你也可以使用 `dart:developer` 中的 `log` 来输出日志。 + +#### Flutter + +在 Flutter 中你应该使用 `debugPrint` 来打印日志。 + +这样也会让调试日志能够使用 `flutter logs` 获取到。 + +**注意:** `debugPrint` 的意义 **不是只在 DEBUG 模式下打印**, +而是对输出内容进行节流,从而保证输出完整。 +请不要在生产模式使用,除非你有意输出相关日志。 ```dart dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); ``` -**注意:** 由于拦截器队列是先进先出,`LogInterceptor` 应当在最后添加至 `Dio` 实例。 - -**注意:** 日志默认仅在 DEBUG 模式(启用了断言)下打印。 - ### 自定义拦截器 开发者可以通过继承 `Interceptor/QueuedInterceptor` 类来实现自定义拦截器。 From edc6823788bdecccb7cbb2a95fddb48d622b0bdc Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 28 Sep 2023 10:01:52 +0800 Subject: [PATCH 5/7] Update README-ZH.md --- dio/README-ZH.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/dio/README-ZH.md b/dio/README-ZH.md index 73049c83d..0e06ee863 100644 --- a/dio/README-ZH.md +++ b/dio/README-ZH.md @@ -480,26 +480,27 @@ print(response.data); // 'fake data' dio.interceptors.add(LogInterceptor(responseBody: false)); // 不输出响应内容体 ``` -**Note:** When using the default `logPrint` function, logs will only be printed -in DEBUG mode (when the assertion is enabled). - **注意:** 默认的 `logPrint` 只会在 DEBUG 模式(启用了断言) 的情况下输出日志。 -你也可以使用 `dart:developer` 中的 `log` 来输出日志。 +你也可以使用 `dart:developer` 中的 `log` 来输出日志(在 Flutter 中也可以使用)。 #### Flutter 在 Flutter 中你应该使用 `debugPrint` 来打印日志。 -这样也会让调试日志能够使用 `flutter logs` 获取到。 +这样也会让调试日志能够通过 `flutter logs` 获取到。 **注意:** `debugPrint` 的意义 **不是只在 DEBUG 模式下打印**, 而是对输出内容进行节流,从而保证输出完整。 请不要在生产模式使用,除非你有意输出相关日志。 ```dart -dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); +dio.interceptors.add( + LogInterceptor( + logPrint: (o) => debugPrint(o.toString()), + ), +); ``` ### 自定义拦截器 From 308ab8559b7243dd3373f441d3b894b76fe538c5 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 28 Sep 2023 10:06:10 +0800 Subject: [PATCH 6/7] Update README.md --- dio/README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/dio/README.md b/dio/README.md index 2b0c51384..a77ef77b4 100644 --- a/dio/README.md +++ b/dio/README.md @@ -498,17 +498,24 @@ dio.interceptors.add(LogInterceptor(responseBody: false)); // Do not output resp **Note:** When using the default `logPrint` function, logs will only be printed in DEBUG mode (when the assertion is enabled). - -Alternatively `dart:developer`'s log can also be used to log messages. +Alternatively `dart:developer`'s log can also be used to log messages (available in Flutter too). #### Flutter When using Flutter, Flutters own `debugPrint` function should be used. + This ensures, that debug messages are also available via `flutter logs`. +**Note:** `debugPrint` **does not mean print logs under the DEBUG mode**, +it's a throttled function which helps to print full logs without truncation. +Do not use it under any production environment unless you're intended to. ```dart -dio.interceptors.add(LogInterceptor(logPrint: (o) => debugPrint(o.toString()))); +dio.interceptors.add( + LogInterceptor( + logPrint: (o) => debugPrint(o.toString()), + ), +); ``` #### Custom Interceptor From a7e136865a11b67d8c85c700ee0277e43b3c59d2 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 28 Sep 2023 10:35:35 +0800 Subject: [PATCH 7/7] Update log.dart --- dio/lib/src/interceptors/log.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dio/lib/src/interceptors/log.dart b/dio/lib/src/interceptors/log.dart index fc2487d4b..682384334 100644 --- a/dio/lib/src/interceptors/log.dart +++ b/dio/lib/src/interceptors/log.dart @@ -1,12 +1,10 @@ -import 'dart:developer'; - import '../dio_exception.dart'; import '../dio_mixin.dart'; import '../options.dart'; import '../response.dart'; /// [LogInterceptor] is used to print logs during network requests. -/// [LogInterceptor] should be the last interceptor added, +/// It should be the last interceptor added, /// otherwise modifications by following interceptors will not be logged. /// This is because the execution of interceptors is in the order of addition. /// @@ -15,9 +13,11 @@ import '../response.dart'; /// Alternatively `dart:developer`'s `log` function can also be used. /// /// ```dart -/// dio.interceptors.add(LogInterceptor( -/// logPrint: (o) => debugPrint(o.toString()), -/// )); +/// dio.interceptors.add( +/// LogInterceptor( +/// logPrint: (o) => debugPrint(o.toString()), +/// ), +/// ); /// ``` class LogInterceptor extends Interceptor { LogInterceptor({