Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for LogInterceptor #1971

Merged
merged 7 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,39 @@ print(response.data); // 'fake data'

#### 日志拦截器

我们可以添加 `LogInterceptor` 拦截器来自动打印请求、响应日志:
我们可以添加 `LogInterceptor` 拦截器来自动打印请求和响应等日志:

**注意:** `LogInterceptor` 应该保持最后一个被添加到拦截器中,
否则在它之后进行处理的拦截器修改的内容将无法体现。

#### Dart

```dart
dio.interceptors.add(LogInterceptor(responseBody: false)); // 不输出响应内容体
```

**注意:** 由于拦截器队列是先进先出,`LogInterceptor` 应当在最后添加至 `Dio` 实例。
**注意:** 默认的 `logPrint` 只会在 DEBUG 模式(启用了断言)
的情况下输出日志。

你也可以使用 `dart:developer` 中的 `log` 来输出日志(在 Flutter 中也可以使用)。

#### Flutter

在 Flutter 中你应该使用 `debugPrint` 来打印日志。

这样也会让调试日志能够通过 `flutter logs` 获取到。

**注意:** 日志默认仅在 DEBUG 模式(启用了断言)下打印。
**注意:** `debugPrint` 的意义 **不是只在 DEBUG 模式下打印**,
而是对输出内容进行节流,从而保证输出完整。
请不要在生产模式使用,除非你有意输出相关日志。

```dart
dio.interceptors.add(
LogInterceptor(
logPrint: (o) => debugPrint(o.toString()),
),
);
```

### 自定义拦截器

Expand Down
30 changes: 27 additions & 3 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,39 @@ 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.
```

**Note:** `LogInterceptor` should be the last to add since the interceptors are FIFO.
**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 (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:** Logs will only be printed in the DEBUG mode (when the assertion is enabled).
**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()),
),
);
```

#### Custom Interceptor

Expand Down
16 changes: 14 additions & 2 deletions dio/lib/src/interceptors/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ 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.
/// 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.
///
/// **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(
/// logPrint: (o) => debugPrint(o.toString()),
/// ),
/// );
/// ```
class LogInterceptor extends Interceptor {
LogInterceptor({
this.request = true,
Expand Down