Skip to content

Commit

Permalink
✨ mica-http 优化,可以在没有 slf4j 的情况下开启控制台日志
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Dec 1, 2023
1 parent bdf6ba5 commit 264e327
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.print("ConsoleLogger: ");
System.out.println(message);
}

}
18 changes: 15 additions & 3 deletions mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
Original file line number Diff line number Diff line change
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 264e327

Please sign in to comment.