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

Not an issue, just a question #3

Closed
AliPunjabi opened this issue Jan 28, 2022 · 2 comments
Closed

Not an issue, just a question #3

AliPunjabi opened this issue Jan 28, 2022 · 2 comments

Comments

@AliPunjabi
Copy link

is there anyway to print all this data in log?

@themisir
Copy link
Owner

themisir commented Jan 28, 2022

You can listen to network events stream - NetworkLogger.instance.stream and log all required data. But I would personally suggest creating a Dio interceptor to log data from dio instances, which could help you to decouple network monitoring from logging and reduce dependency on this package.

Here's a logging interceptor I've used for one of our projects, you can either re-use it or slightly modify for your needs.

/// Verbosely logs HTTP request and response metadata and any errors ocurred
/// during the operation
class DioLoggerInterceptor extends Interceptor {
  final _log = Logger('Dio');

  String _requestUrl(RequestOptions options) {
    return '${options.uri}';
  }

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    _log.fine('---> ${options.method} ${_requestUrl(options)}');
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    _log.fine(
      '<--- ${response.requestOptions.method} '
      '${_requestUrl(response.requestOptions)} '
      '[${response.statusCode}]',
    );
    super.onResponse(response, handler);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    _log.shout(
      '-!!!- ${err.requestOptions.method} ${_requestUrl(err.requestOptions)} '
      '[${err.response?.statusCode ?? -1}]',
    );
    // Ignore authentication errors because those errors might ocurr when token
    // expires which will be refreshed automatically and request will be sent
    // again.
    if (err.response?.statusCode != 401)
      _log.shout(err.message, err, err.stackTrace);
    super.onError(err, handler);
  }
}

PS: Logger on above code snippet is imported from logging package.

@AliPunjabi
Copy link
Author

Thankyou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants