Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.7.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	mica-bom/build.gradle
  • Loading branch information
li-xunhuan committed Dec 1, 2023
2 parents 46917d7 + cd67de9 commit be7d374
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2019-2029, Dreamlu ([email protected] & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dreamlu.mica.http;

import okhttp3.logging.HttpLoggingInterceptor;

import javax.annotation.Nonnull;

/**
* OkHttp console log.
*
* @author L.cm
*/
public enum HttpConsoleLogger implements HttpLoggingInterceptor.Logger {
/**
* 实例
*/
INSTANCE;

public void log(@Nonnull String message) {
// 统一添加前缀,方便在茫茫日志中查看
System.out.printf("HttpLogger: %s\n", message);
}

}
20 changes: 16 additions & 4 deletions mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* @author L.cm
*/
public class HttpRequest {
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0";
private static final MediaType APPLICATION_JSON = MediaType.get("application/json");
private static volatile OkHttpClient httpClient = new OkHttpClient();
@Nullable
Expand Down Expand Up @@ -422,15 +422,27 @@ public HttpRequest useSlf4jLog() {
}

public HttpRequest useSlf4jLog(LogLevel logLevel) {
return useLog(HttpLogger.Slf4j, logLevel);
return useLog(HttpSel4jLogger.INSTANCE, logLevel);
}

public HttpRequest useConsoleLog() {
return useConsoleLog(LogLevel.BODY);
}

public HttpRequest useConsoleLog(LogLevel logLevel) {
return useLog(HttpLogger.Console, logLevel);
return useLog(HttpConsoleLogger.INSTANCE, logLevel);
}

public HttpRequest useDefaultLog() {
return useDefaultLog(LogLevel.BODY);
}

public HttpRequest useDefaultLog(LogLevel logLevel) {
return useLog(HttpLoggingInterceptor.Logger.DEFAULT, logLevel);
}

public HttpRequest useLog(HttpLoggingInterceptor.Logger logger) {
return useLog(logger, LogLevel.BODY);
}

public HttpRequest useLog(HttpLoggingInterceptor.Logger logger, LogLevel logLevel) {
Expand Down Expand Up @@ -570,7 +582,7 @@ public static void setHttpClient(OkHttpClient httpClient) {
* @param logLevel LogLevel
*/
public static void setGlobalLog(LogLevel logLevel) {
setGlobalLog(HttpLogger.Slf4j, logLevel);
setGlobalLog(HttpSel4jLogger.INSTANCE, logLevel);
}

public static void setGlobalLog(HttpLoggingInterceptor.Logger logger, LogLevel logLevel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,21 @@
import javax.annotation.Nonnull;

/**
* OkHttp logger, Slf4j and console log.
* OkHttp Slf4j log.
*
* @author L.cm
*/
@Slf4j
public enum HttpLogger implements HttpLoggingInterceptor.Logger {
public enum HttpSel4jLogger implements HttpLoggingInterceptor.Logger {

/**
* http 日志:Slf4j
* 实例
*/
Slf4j() {
@Override
public void log(@Nonnull String message) {
log.info(message);
}
},
INSTANCE;

/**
* http 日志:Console
*/
Console() {
@Override
public void log(@Nonnull String message) {
// 统一添加前缀,方便在茫茫日志中查看
System.out.print("ConsoleLogger: ");
System.out.println(message);
}
};
@Override
public void log(@Nonnull String message) {
log.info(message);
}

}

0 comments on commit be7d374

Please sign in to comment.