From 55ee81f40aa57d45e3d0cb604b70f4e73ef454e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C7=B3=C3=83=C3=8E?= <1101766085@qq.com> Date: Fri, 1 Dec 2023 14:15:33 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20mica-http=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=9C=A8=E6=B2=A1=E6=9C=89=20slf4j?= =?UTF-8?q?=20=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{HttpSel4jLogger.java => HttpLogger.java} | 27 +++++++++++++------ .../net/dreamlu/mica/http/HttpRequest.java | 22 +++++++++++++-- 2 files changed, 39 insertions(+), 10 deletions(-) rename mica-http/src/main/java/net/dreamlu/mica/http/{HttpSel4jLogger.java => HttpLogger.java} (64%) diff --git a/mica-http/src/main/java/net/dreamlu/mica/http/HttpSel4jLogger.java b/mica-http/src/main/java/net/dreamlu/mica/http/HttpLogger.java similarity index 64% rename from mica-http/src/main/java/net/dreamlu/mica/http/HttpSel4jLogger.java rename to mica-http/src/main/java/net/dreamlu/mica/http/HttpLogger.java index 7ae9e4fbb..3dc8d8fba 100644 --- a/mica-http/src/main/java/net/dreamlu/mica/http/HttpSel4jLogger.java +++ b/mica-http/src/main/java/net/dreamlu/mica/http/HttpLogger.java @@ -22,21 +22,32 @@ import javax.annotation.Nonnull; /** - * OkHttp Slf4j log. + * OkHttp logger, Slf4j and console log. * * @author L.cm */ @Slf4j -public enum HttpSel4jLogger implements HttpLoggingInterceptor.Logger { +public enum HttpLogger implements HttpLoggingInterceptor.Logger { /** - * 实例 + * http 日志:Slf4j */ - INSTANCE; + Slf4j() { + @Override + public void log(@Nonnull String message) { + log.info(message); + } + }, - @Override - public void log(@Nonnull String message) { - log.info(message); - } + /** + * http 日志:Console + */ + Console() { + @Override + public void log(@Nonnull String message) { + // 统一添加前缀,方便在茫茫日志中查看 + System.out.printf("HttpLogger: %s\n", message); + } + }; } diff --git a/mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java b/mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java index 4b0fbd31f..5120d503f 100644 --- a/mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java +++ b/mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java @@ -422,7 +422,7 @@ public HttpRequest useSlf4jLog() { } public HttpRequest useSlf4jLog(LogLevel logLevel) { - return useLog(HttpSel4jLogger.INSTANCE, logLevel); + return useLog(HttpLogger.Slf4j, logLevel); } public HttpRequest useConsoleLog() { @@ -582,7 +582,25 @@ public static void setHttpClient(OkHttpClient httpClient) { * @param logLevel LogLevel */ public static void setGlobalLog(LogLevel logLevel) { - setGlobalLog(HttpSel4jLogger.INSTANCE, logLevel); + setGlobalLog(HttpLogger.Slf4j, logLevel); + } + + /** + * 设置全局日志,平台自带日志,默认 jdk 日志 + * + * @param logLevel LogLevel + */ + public static void setGlobalDefaultLog(LogLevel logLevel) { + setGlobalLog(HttpLoggingInterceptor.Logger.DEFAULT, logLevel); + } + + /** + * 设置全局日志,控制台日志 + * + * @param logLevel LogLevel + */ + public static void setGlobalConsoleLog(LogLevel logLevel) { + setGlobalLog(HttpConsoleLogger.INSTANCE, logLevel); } public static void setGlobalLog(HttpLoggingInterceptor.Logger logger, LogLevel logLevel) {