feat: 优化日志打印

This commit is contained in:
zengxiaobo 2024-05-22 14:14:50 +08:00
parent 0ee4e1bf4f
commit 627fa77325

View File

@ -13,10 +13,7 @@ import lombok.Getter;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@ -26,8 +23,10 @@ public class RequestParams {
private static final String AUTHORIZATION_HEADER_NAME = "Axzo-Authorization";
private static final int MAX_REQUEST_BODY_LOG_SIZE = 1024;
private static final ImmutableSet<String> PRETTY_PRINT_HEADERS = ImmutableSet.of("userinfo");
private static final ImmutableSet<String> IGNORE_PRINT_HEADERS = ImmutableSet.of(AUTHORIZATION_HEADER_NAME, "User-Agent");
private static final Set<String> PRETTY_PRINT_HEADERS = ImmutableSet.of("userinfo");
private static final Set<String> IGNORE_PRINT_HEADERS = ImmutableSet.of(AUTHORIZATION_HEADER_NAME, "User-Agent", "authorization").stream()
.map(StringUtils::toRootLowerCase).collect(Collectors.toSet());
@Getter
Map<String, String> headers;
@ -62,7 +61,8 @@ public class RequestParams {
@Override
public String toString() {
Object data = contentType == ContentType.FORM ? ((FormParams) this).getParams(entry -> !(entry.getValue() instanceof byte[])) : this.getData();
Map<String, String> printHeaders = Maps.filterKeys(Optional.ofNullable(headers).orElse(Collections.emptyMap()), e -> !IGNORE_PRINT_HEADERS.contains(e));
Map<String, String> printHeaders = Maps.filterKeys(Optional.ofNullable(headers)
.orElse(Collections.emptyMap()), e -> !IGNORE_PRINT_HEADERS.contains(StringUtils.toRootLowerCase(e)));
Map<String, String> shortHeaders = Maps.transformEntries(printHeaders, (k, v) -> {
String value = v;
// 部分header 太长不方便日志, 截断.