From b72afb86c18d013c734623ad4ff114c92928a696 Mon Sep 17 00:00:00 2001 From: Hason <258831020@qq.com> Date: Thu, 6 Jun 2024 23:52:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E8=87=AA=E5=AE=9A=E4=B9=89=E6=97=A5=E5=BF=97?= =?UTF-8?q?=20logger=20=E5=90=8D=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cherry-pick from master 2376d197ceff1411d32313395b95dd8c2f7877ad --- .../spring/boot/log/GlobalLogProperty.java | 5 + .../retrofit/spring/boot/log/Logging.java | 10 ++ .../spring/boot/log/LoggingInterceptor.java | 11 +- .../integration/log/CustomLoggerTest.java | 101 ++++++++++++++++++ .../log/CustomLoggerUserService.java | 37 +++++++ .../log/DefaultLoggerUserService.java | 21 ++++ 6 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerTest.java create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerUserService.java create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/DefaultLoggerUserService.java diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/GlobalLogProperty.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/GlobalLogProperty.java index 7c0cde5..48509e6 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/GlobalLogProperty.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/GlobalLogProperty.java @@ -14,6 +14,11 @@ public class GlobalLogProperty { */ private boolean enable = true; + /** + * logger 名字,默认为{@link LoggingInterceptor} 的全类名 + */ + private String logger = LoggingInterceptor.class.getName(); + /** * 日志打印级别,支持的日志级别参见{@link LogLevel} * Log printing level, see {@link LogLevel} for supported log levels diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/Logging.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/Logging.java index 6f85f86..8f43dc1 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/Logging.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/Logging.java @@ -24,6 +24,16 @@ */ boolean enable() default true; + /** + * 设置 logger 名字,效果相当于 {@code LoggerFactory.getLogger(logger)}。 + *
+ * 默认值为 {@link LoggingInterceptor} 的全类名。 + *
如果为空,使用默认值。
+ * + * @return logger 名字 + */ + String logger() default ""; + /** * 日志打印级别,支持的日志级别参见{@link LogLevel} * 如果为NULL,则取全局日志打印级别 diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/LoggingInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/LoggingInterceptor.java index 342abc9..6d984fe 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/LoggingInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/LoggingInterceptor.java @@ -1,11 +1,12 @@ package com.github.lianjiatech.retrofit.spring.boot.log; import com.github.lianjiatech.retrofit.spring.boot.util.AnnotationExtendUtils; -import lombok.extern.slf4j.Slf4j; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import retrofit2.Invocation; import java.io.IOException; @@ -14,7 +15,6 @@ * @author 陈添明 * @since 2022/4/30 8:21 下午 */ -@Slf4j public class LoggingInterceptor implements Interceptor { protected final GlobalLogProperty globalLogProperty; @@ -33,9 +33,11 @@ public Response intercept(Chain chain) throws IOException { if (logStrategy == LogStrategy.NONE) { return chain.proceed(chain.request()); } + LogLevel logLevel = logging == null ? globalLogProperty.getLogLevel() : logging.logLevel(); boolean aggregate = logging == null ? globalLogProperty.isAggregate() : logging.aggregate(); - HttpLoggingInterceptor.Logger matchLogger = matchLogger(logLevel); + String loggerName = logging == null || logging.logger().isEmpty() ? globalLogProperty.getLogger() : logging.logger(); + HttpLoggingInterceptor.Logger matchLogger = matchLogger(loggerName, logLevel); HttpLoggingInterceptor.Logger logger = aggregate ? new BufferingLogger(matchLogger) : matchLogger; HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(logger) .setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name())); @@ -66,7 +68,8 @@ protected boolean needLog(Logging logging) { } } - protected HttpLoggingInterceptor.Logger matchLogger(LogLevel level) { + protected HttpLoggingInterceptor.Logger matchLogger(String loggerName, LogLevel level) { + Logger log = LoggerFactory.getLogger(loggerName); if (level == LogLevel.DEBUG) { return log::debug; } else if (level == LogLevel.ERROR) { diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerTest.java new file mode 100644 index 0000000..d4c3e7b --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerTest.java @@ -0,0 +1,101 @@ +package com.github.lianjiatech.retrofit.spring.boot.test.integration.log; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; +import com.github.lianjiatech.retrofit.spring.boot.log.GlobalLogProperty; +import com.github.lianjiatech.retrofit.spring.boot.log.Logging; +import com.github.lianjiatech.retrofit.spring.boot.test.integration.MockWebServerTest; +import com.github.lianjiatech.retrofit.spring.boot.test.integration.RetrofitBootApplication; +import com.github.lianjiatech.retrofit.spring.boot.test.integration.entity.User; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@link Logging#logger()} based on logback + * + * @author Hason + */ +@SpringBootTest(classes = {RetrofitBootApplication.class}) +@RunWith(SpringRunner.class) +public class CustomLoggerTest extends MockWebServerTest { + + @Autowired + private CustomLoggerUserService service; + + @Autowired + private DefaultLoggerUserService defaultLoggerUserService; + + private TestAppender testAppender; + + @Before + public void setup() { + testAppender = new TestAppender(); + } + + @Test + public void shouldEqualsDefaultLogger() { + // given + GlobalLogProperty property = new GlobalLogProperty(); + Logger logger = (Logger) LoggerFactory.getLogger(property.getLogger()); + logger.addAppender(testAppender); + testAppender.start(); + + mockServerReturnString(MIKE); + + // when + defaultLoggerUserService.getName(Long100); + + // then 应当使用类的logger打印日志 + assertEquals(property.getLogger(), testAppender.lastEvent.getLoggerName()); + } + + @Test + public void shouldEqualsClassLogger() { + // given + Logger logger = (Logger) LoggerFactory.getLogger(CustomLoggerUserService.LOGGER); + logger.addAppender(testAppender); + testAppender.start(); + + mockServerReturnString(MIKE); + + // when + service.getName(Long100); + + // then 应当使用类的logger打印日志 + assertEquals(CustomLoggerUserService.LOGGER, testAppender.lastEvent.getLoggerName()); + } + + @Test + public void shouldEqualsMethodLogger() { + // given + Logger logger = (Logger) LoggerFactory.getLogger(CustomLoggerUserService.LOGGER); + logger.addAppender(testAppender); + testAppender.start(); + + mockServerReturnObject(USER_MIKE); + + // when + User user = service.getUser(Long100); + + // then 应当使用类的logger打印日志 + assertEquals(CustomLoggerUserService.LOGGER + ".getUser", testAppender.lastEvent.getLoggerName()); + } + + static class TestAppender extends AppenderBase