Milad-Akarie / pretty_dio_logger

MIT License
237 stars 53 forks source link

Feature request: use extra for enable logger only for specific request #49

Open lukas-pierce opened 2 months ago

lukas-pierce commented 2 months ago

I use a logger inside my api class, and when I enable it, too much unnecessary information gets into the console

I know that it is possible to filter requests using the filter function, but it is difficult to write a condition every time to debug a new request

But it would be great to add the ability to specify for a specific request whether it should be logged or not using the extra option

And you could also add the ability to overwrite the default config for a specific request

The concept could ultimately look like this:

class MyBigApiClass {

  myApiMethod1() async {
    final response = await dio.get(
      '/api/foo/bar',
      options: Options(extra: {

        // enable logging for this request only
        'prettyLog': true,

      }),
    );
  }

  myApiMethod2() async {
    final response = await dio.get(
      '/api/foo/bar',
      options: Options(extra: {

        // disable logging for this request only
        'prettyLog': false,

      }),
    );
  }

  myApiMethod3() async {
    final response = await dio.get(
      '/api/foo/bar',
      options: Options(extra: {

        // costomize logging for this request only
        'prettyLog': PrettyDioOverrideConfig(
          enabled: true,
          request: false,
          requestHeader: false,
          requestBody: true,
          responseHeader: true,
          responseBody: false,
          error: true,
        )

      }),
    );
  }
}
chenjiangmin commented 4 days ago

这个可以通过自己过滤一下就行了

    /// 添加日志拦截器
    dio.interceptors.add(PrettyDioLogger(
      requestHeader: true,
      requestBody: true,
      responseBody: true,
      responseHeader: false,
      error: true,
      compact: true,
      enabled: kDebugMode,
      filter: (options, args) {
        if (options.extra['prettyLog'] == true) return true;
        return false;
      },
    ));
lukas-pierce commented 23 hours ago

@chenjiangmin Yes, you specified it only for filtering. But I would also like to customize the logging parameters at the extra options level. See myApiMethod2 and myApiMethod3 in my example