1
1
package com .aries .library .fast .retrofit ;
2
2
3
3
import android .text .TextUtils ;
4
- import android .util .Log ;
4
+
5
+ import androidx .annotation .Nullable ;
5
6
6
7
import com .aries .library .fast .manager .LoggerManager ;
7
8
import com .aries .library .fast .util .SSLUtil ;
15
16
import javax .net .ssl .SSLSocketFactory ;
16
17
import javax .net .ssl .X509TrustManager ;
17
18
18
- import androidx .annotation .Nullable ;
19
19
import io .reactivex .Observable ;
20
- import io .reactivex .functions .Consumer ;
21
20
import io .reactivex .schedulers .Schedulers ;
22
21
import okhttp3 .Interceptor ;
23
22
import okhttp3 .OkHttpClient ;
@@ -78,6 +77,10 @@ public class FastRetrofit {
78
77
* 证书配置
79
78
*/
80
79
private SSLUtil .SSLParams mSslParams = new SSLUtil .SSLParams ();
80
+ /**
81
+ * 日志tag
82
+ */
83
+ private String mLogTag = "FastRetrofit" ;
81
84
/**
82
85
* 日志拦截器
83
86
*/
@@ -211,13 +214,10 @@ public Observable<ResponseBody> downloadFile(String fileUrl, Map<String, Object>
211
214
FastRetrofit .getInstance ().setLogEnable (false );
212
215
return FastRetrofit .getRetrofit ()
213
216
.create (FastRetrofitService .class )
214
- .downloadFile (fileUrl , header == null ? new HashMap <String , Object >(0 ) : header )
215
- .doOnNext (new Consumer <ResponseBody >() {
216
- @ Override
217
- public void accept (ResponseBody responseBody ) {
218
- //onNext回调前还原log状态
219
- FastRetrofit .getInstance ().setLogEnable (logEnable );
220
- }
217
+ .downloadFile (fileUrl , header == null ? new HashMap <>(0 ) : header )
218
+ .doOnNext (responseBody -> {
219
+ //onNext回调前还原log状态
220
+ FastRetrofit .getInstance ().setLogEnable (logEnable );
221
221
})
222
222
.subscribeOn (Schedulers .io ());
223
223
}
@@ -244,7 +244,7 @@ public Observable<ResponseBody> uploadFile(String uploadUrl, @Nullable RequestBo
244
244
public Observable <ResponseBody > uploadFile (String uploadUrl , @ Nullable final RequestBody body , Map <String , Object > header ) {
245
245
return getRetrofit ()
246
246
.create (FastRetrofitService .class )
247
- .uploadFile (uploadUrl , body , header == null ? new HashMap <>() : header )
247
+ .uploadFile (uploadUrl , body , header == null ? new HashMap <>(0 ) : header )
248
248
.compose (FastTransformer .<ResponseBody >switchSchedulers ());
249
249
}
250
250
@@ -406,11 +406,22 @@ public boolean isLogEnable() {
406
406
/**
407
407
* 设置日志打印
408
408
*
409
- * @param enable
409
+ * @param enable 是否打印日志
410
410
* @return
411
411
*/
412
412
public FastRetrofit setLogEnable (boolean enable ) {
413
- return setLogEnable (enable , this .getClass ().getSimpleName (), HttpLoggingInterceptor .Level .BODY );
413
+ return setLogEnable (enable , mLogTag );
414
+ }
415
+
416
+ /**
417
+ * 设置日志打印
418
+ *
419
+ * @param enable 是否打印日志
420
+ * @param tag 日志标签
421
+ * @return
422
+ */
423
+ public FastRetrofit setLogEnable (boolean enable , String tag ) {
424
+ return setLogEnable (enable , tag , HttpLoggingInterceptor .Level .BODY );
414
425
}
415
426
416
427
/**
@@ -421,27 +432,25 @@ public FastRetrofit setLogEnable(boolean enable) {
421
432
* @return
422
433
*/
423
434
public FastRetrofit setLogEnable (boolean enable , String tag , HttpLoggingInterceptor .Level level ) {
435
+ tag = TextUtils .isEmpty (tag ) ? mLogTag : tag ;
424
436
if (TextUtils .isEmpty (tag )) {
425
- tag = getClass (). getSimpleName () ;
437
+ tag = "FastRetrofit" ;
426
438
}
439
+ mLogTag = tag ;
427
440
if (enable ) {
428
441
if (mLoggingInterceptor == null ) {
429
- final String finalTag = tag ;
430
- mLoggingInterceptor = new HttpLoggingInterceptor (new HttpLoggingInterceptor .Logger () {
431
- @ Override
432
- public void log (String message ) {
433
- if (TextUtils .isEmpty (message )) {
434
- return ;
435
- }
436
- //json格式使用Logger.json打印
437
- boolean isJson = message .startsWith ("[" ) || message .startsWith ("{" );
438
- isJson = isJson && mLogJsonEnable ;
439
- if (isJson ) {
440
- LoggerManager .json (finalTag , message );
441
- return ;
442
- }
443
- Log .d (finalTag , message );
442
+ mLoggingInterceptor = new HttpLoggingInterceptor (message -> {
443
+ if (TextUtils .isEmpty (message )) {
444
+ return ;
445
+ }
446
+ //json格式使用Logger.json打印
447
+ boolean isJson = message .startsWith ("[" ) || message .startsWith ("{" );
448
+ isJson = isJson && mLogJsonEnable ;
449
+ if (isJson ) {
450
+ LoggerManager .json (mLogTag , message );
451
+ return ;
444
452
}
453
+ LoggerManager .d (mLogTag , message );
445
454
});
446
455
}
447
456
mLoggingInterceptor .setLevel (level );
0 commit comments