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