Skip to content

Commit

Permalink
Improve docs for LogInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal committed Sep 18, 2023
1 parent 22cfd6b commit b1fe06b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 模式(启用了断言)下打印。
Expand Down
9 changes: 8 additions & 1 deletion dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
13 changes: 11 additions & 2 deletions dio/lib/src/interceptors/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b1fe06b

Please sign in to comment.