Line data Source code
1 : import '../dio_error.dart'; 2 : import '../dio_mixin.dart'; 3 : import '../options.dart'; 4 : import '../response.dart'; 5 : 6 : /// [LogInterceptor] is used to print logs during network requests. 7 : /// It's better to add [LogInterceptor] to the tail of the interceptor queue, 8 : /// otherwise the changes made in the interceptor behind A will not be printed out. 9 : /// This is because the execution of interceptors is in the order of addition. 10 : class LogInterceptor extends Interceptor { 11 1 : LogInterceptor({ 12 : this.request = true, 13 : this.requestHeader = true, 14 : this.requestBody = false, 15 : this.responseHeader = true, 16 : this.responseBody = false, 17 : this.error = true, 18 : this.logPrint = print, 19 : }); 20 : 21 : /// Print request [Options] 22 : bool request; 23 : 24 : /// Print request header [Options.headers] 25 : bool requestHeader; 26 : 27 : /// Print request data [Options.data] 28 : bool requestBody; 29 : 30 : /// Print [Response.data] 31 : bool responseBody; 32 : 33 : /// Print [Response.headers] 34 : bool responseHeader; 35 : 36 : /// Print error message 37 : bool error; 38 : 39 : /// Log printer; defaults print log to console. 40 : /// In flutter, you'd better use debugPrint. 41 : /// you can also write log in a file, for example: 42 : ///```dart 43 : /// var file=File("./log.txt"); 44 : /// var sink=file.openWrite(); 45 : /// dio.interceptors.add(LogInterceptor(logPrint: sink.writeln)); 46 : /// ... 47 : /// await sink.close(); 48 : ///``` 49 : void Function(Object object) logPrint; 50 : 51 : @override 52 1 : void onRequest( 53 : RequestOptions options, RequestInterceptorHandler handler) async { 54 1 : logPrint('*** Request ***'); 55 2 : _printKV('uri', options.uri); 56 : //options.headers; 57 : 58 1 : if (request) { 59 2 : _printKV('method', options.method); 60 3 : _printKV('responseType', options.responseType.toString()); 61 2 : _printKV('followRedirects', options.followRedirects); 62 2 : _printKV('connectTimeout', options.connectTimeout); 63 2 : _printKV('sendTimeout', options.sendTimeout); 64 2 : _printKV('receiveTimeout', options.receiveTimeout); 65 1 : _printKV( 66 1 : 'receiveDataWhenStatusError', options.receiveDataWhenStatusError); 67 2 : _printKV('extra', options.extra); 68 : } 69 1 : if (requestHeader) { 70 1 : logPrint('headers:'); 71 5 : options.headers.forEach((key, v) => _printKV(' $key', v)); 72 : } 73 1 : if (requestBody) { 74 1 : logPrint('data:'); 75 2 : _printAll(options.data); 76 : } 77 1 : logPrint(''); 78 : 79 1 : handler.next(options); 80 : } 81 : 82 : @override 83 1 : void onResponse(Response response, ResponseInterceptorHandler handler) async { 84 1 : logPrint('*** Response ***'); 85 1 : _printResponse(response); 86 1 : handler.next(response); 87 : } 88 : 89 : @override 90 1 : void onError(DioError err, ErrorInterceptorHandler handler) async { 91 1 : if (error) { 92 1 : logPrint('*** DioError ***:'); 93 4 : logPrint('uri: ${err.requestOptions.uri}'); 94 2 : logPrint('$err'); 95 1 : if (err.response != null) { 96 2 : _printResponse(err.response!); 97 : } 98 1 : logPrint(''); 99 : } 100 : 101 1 : handler.next(err); 102 : } 103 : 104 1 : void _printResponse(Response response) { 105 3 : _printKV('uri', response.requestOptions.uri); 106 1 : if (responseHeader) { 107 2 : _printKV('statusCode', response.statusCode); 108 2 : if (response.isRedirect == true) { 109 2 : _printKV('redirect', response.realUri); 110 : } 111 : 112 1 : logPrint('headers:'); 113 6 : response.headers.forEach((key, v) => _printKV(' $key', v.join('\r\n\t'))); 114 : } 115 1 : if (responseBody) { 116 1 : logPrint('Response Text:'); 117 2 : _printAll(response.toString()); 118 : } 119 1 : logPrint(''); 120 : } 121 : 122 1 : void _printKV(String key, Object? v) { 123 2 : logPrint('$key: $v'); 124 : } 125 : 126 1 : void _printAll(msg) { 127 4 : msg.toString().split('\n').forEach(logPrint); 128 : } 129 : }