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,