Skip to content

Commit

Permalink
✨ 代码完善,方便使用
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Dec 15, 2023
1 parent 81fc56a commit b7ad747
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
6 changes: 4 additions & 2 deletions mica-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
compile("net.dreamlu:mica-http:${version}")
```

spring-retry 为可选依赖,用来对 http 结果断言重试。
注意:**2.5.7 开始已经不再需要** spring-retry 为可选依赖,用来对 http 结果断言重试。
```groovy
compile("org.springframework.retry:spring-retry:${springRetryVersion}")
```
Expand All @@ -36,12 +36,14 @@ compile("org.springframework.retry:spring-retry:${springRetryVersion}")
HttpRequest.setGlobalLog(LogLevel.BODY);
// 设置控制台日志,用于没有日志依赖的 sdk 开发时使用
HttpRequest.setGlobalLog(HttpLogger.Console, LogLevel.BODY);
// 当然你也可以设定为自己的 log,这样就不用把 **net.dreamlu.mica.http** 包的日志设置为 info 级别。
HttpRequest.setGlobalLog(log::info);
```

```java
// 同步请求 url,方法支持 get、post、patch、put、delete
HttpRequest.get("https://www.baidu.com")
.useSlf4jLog() // 使用 Slf4j 日志,同类的有 .useConsoleLog(),日志级别为 BODY
.useSlf4jLog() // 使用 Slf4j 日志,同类的有 .useConsoleLog(),日志级别为 BODY 和 自定义的 .useLog(log::info)
.addHeader("x-account-id", "mica001") // 添加 header
.addCookie(builder -> builder.domain("www.baidu.com").name("name").value("value")) // 添加 cookie
.query("q", "mica") // 设置 url 参数,默认进行 url encode
Expand Down
33 changes: 24 additions & 9 deletions mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,6 @@ public static void setHttpClient(OkHttpClient httpClient) {
HttpRequest.httpClient = httpClient;
}

/**
* 设置全局日志,默认:Slf4j
*
* @param logLevel LogLevel
*/
public static void setGlobalLog(LogLevel logLevel) {
setGlobalLog(HttpLogger.Slf4j, logLevel);
}

/**
* 设置全局日志,平台自带日志,默认 jdk 日志
*
Expand All @@ -603,6 +594,30 @@ public static void setGlobalConsoleLog(LogLevel logLevel) {
setGlobalLog(HttpConsoleLogger.INSTANCE, logLevel);
}

/**
* 设置全局日志,默认级别:BODY
*
* @param logger HttpLoggingInterceptor.Logger
*/
public static void setGlobalLog(HttpLoggingInterceptor.Logger logger) {
setGlobalLog(logger, LogLevel.BODY);
}

/**
* 设置全局日志,默认:Slf4j
*
* @param logLevel LogLevel
*/
public static void setGlobalLog(LogLevel logLevel) {
setGlobalLog(HttpLogger.Slf4j, logLevel);
}

/**
* 设置全局日志
*
* @param logger HttpLoggingInterceptor.Logger
* @param logLevel LogLevel
*/
public static void setGlobalLog(HttpLoggingInterceptor.Logger logger, LogLevel logLevel) {
HttpRequest.globalLoggingInterceptor = getLoggingInterceptor(logger, logLevel.getLevel());
}
Expand Down

0 comments on commit b7ad747

Please sign in to comment.