I recently started learning About Flutter. When viewing network data, I used the default Log interceptor, LogInterceptor, and found that its logs were not ideal — all on one line and not fully printed. To address these issues, I have a custom LogInterceptor
Take a look at the default interceptor’s log:
I’m used to Logger’s log format, but I can’t accept it. So I want to customize the LogInterceptor by imitating Logger’s log format
class WanLogInterceptor extends Interceptor {
static const String TOP_LINE = "┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─";
static const String START_LINE = "│";
static const String SPLIT_LINE = "| ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄";
static const String END_LINE = "└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─";
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
print(TOP_LINE);
print(START_LINE + "--> " + options.method + "" + options.uri.toString());
print(START_LINE + 'responseType: ' + options.responseType.toString());
print(START_LINE + 'followRedirects: ' + options.followRedirects.toString());
print(START_LINE + 'connectTimeout: ' + options.connectTimeout.toString());
print(START_LINE + 'sendTimeout: ' + options.sendTimeout.toString());
print(START_LINE + 'receiveTimeout: ' + options.receiveTimeout.toString());
print(START_LINE + 'receiveDataWhenStatusError: ' + options.receiveDataWhenStatusError.toString());
print(START_LINE + 'extra: ' + options.extra.toString());
print(START_LINE + "Headers: ");
options.headers.forEach((name, values) {
print(START_LINE + " $name: $values");
});
print(END_LINE);
handler.next(options);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
print(TOP_LINE);
print(START_LINE + "< -" + response.statusCode.toString() + "" + response.requestOptions.uri.toString());
print(SPLIT_LINE);
response.headers.forEach((name, values) {
print(START_LINE + "$name: $values");
});
print(START_LINE + "Response Body:");
var formatJson = JsonUtils.formatJson(response.toString());
var splitJson = formatJson.split("\n");
splitJson.forEach((element) {
print(START_LINE + element);
});
print(END_LINE); handler.next(response); }}Copy the code
Formatting for JSON strings
class JsonUtils {
static const int LEFT_BIG_BRACKET = 123; / / {" "
static const int LEFT_MIDDLE_BRACKET = 91; [" / /"
static const int RIGHT_BIG_BRACKET = 125; / / "}"
static const int RIGHT_MIDDLE_BRACKET = 93; / / "]"
static const int COMMA = 44; / / ","
static const String SPACE = "";
static const String WRAP = "\n";
static void writeSpace(StringBuffer stringBuffer, int writeCount){
for(int i = 1; i <= writeCount ; i++){ stringBuffer.write(SPACE); }}static String formatJson(String jsonStr) {
var stringBuffer = StringBuffer(a);var codeUnits = jsonStr.codeUnits;
var deep = 0;
for(var i = 0 ; i < codeUnits.length ; i++){
var unit = codeUnits[i];
var string = String.fromCharCode(unit);
switch(unit){
case LEFT_BIG_BRACKET | LEFT_MIDDLE_BRACKET : {
deep++;
stringBuffer.write(string);
stringBuffer.write(WRAP);
writeSpace(stringBuffer, deep);
}
break;
case RIGHT_BIG_BRACKET | RIGHT_BIG_BRACKET : {
deep--;
stringBuffer.write(WRAP);
writeSpace(stringBuffer, deep);
stringBuffer.write(string);
}
break;
case COMMA : {
stringBuffer.write(string);
stringBuffer.write(WRAP);
writeSpace(stringBuffer, deep);
}
break;
default:{
stringBuffer.write(string);
}
break; }}returnstringBuffer.toString(); }}Copy the code
Output effect:
That’s the end of it. The written utility class hasn’t been extensively tested and may be buggy. The test address is playing android official website, thank you.