-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from HasonHuang/2.x
feat: 支持使用注解自定义日志 logger 名字
- Loading branch information
Showing
6 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...va/com/github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ILoggingEvent> { | ||
private ILoggingEvent lastEvent; | ||
|
||
@Override | ||
protected void append(ILoggingEvent event) { | ||
this.lastEvent = event; | ||
} | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...github/lianjiatech/retrofit/spring/boot/test/integration/log/CustomLoggerUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.integration.log; | ||
|
||
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; | ||
import com.github.lianjiatech.retrofit.spring.boot.log.LogStrategy; | ||
import com.github.lianjiatech.retrofit.spring.boot.log.Logging; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.integration.entity.User; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.POST; | ||
import retrofit2.http.Query; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 测试自定义 logger 的客户端 | ||
* | ||
* @author Hason | ||
*/ | ||
@RetrofitClient(baseUrl = "${test.baseUrl}") | ||
@Logging(logger = CustomLoggerUserService.LOGGER) | ||
public interface CustomLoggerUserService { | ||
|
||
String LOGGER = "CustomLoggerUserService"; | ||
|
||
/** | ||
* 根据id查询用户姓名 | ||
*/ | ||
@POST("getName") | ||
String getName(@Query("id") Long id); | ||
|
||
/** | ||
* 根据id查询用户信息 | ||
*/ | ||
@GET("getUser") | ||
@Logging(logStrategy = LogStrategy.BODY, logger = CustomLoggerUserService.LOGGER + ".getUser") | ||
User getUser(@Query("id") Long id); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...ithub/lianjiatech/retrofit/spring/boot/test/integration/log/DefaultLoggerUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.integration.log; | ||
|
||
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; | ||
import retrofit2.http.POST; | ||
import retrofit2.http.Query; | ||
|
||
/** | ||
* 测试默认 logger 的客户端 | ||
* | ||
* @author Hason | ||
*/ | ||
@RetrofitClient(baseUrl = "${test.baseUrl}") | ||
public interface DefaultLoggerUserService { | ||
|
||
/** | ||
* 根据id查询用户姓名 | ||
*/ | ||
@POST("getName") | ||
String getName(@Query("id") Long id); | ||
|
||
} |