2727import com .squareup .okhttp .Response ;
2828import com .squareup .okhttp .internal .http .HttpMethod ;
2929import com .squareup .okhttp .logging .HttpLoggingInterceptor ;
30- import com .squareup .okhttp .logging .HttpLoggingInterceptor .Level ;
3130import com .volcengine .auth .Authentication ;
3231import com .volcengine .auth .CredentialProvider ;
32+ import com .volcengine .observability .debugger .LogLevel ;
33+ import com .volcengine .observability .debugger .SdkConfigLog ;
3334import com .volcengine .endpoint .DefaultEndpointProvider ;
3435import com .volcengine .endpoint .EndpointResolver ;
3536import com .volcengine .interceptor .BuildRequestInterceptor ;
4243import com .volcengine .interceptor .SignRequestInterceptor ;
4344import com .volcengine .model .AbstractResponse ;
4445import com .volcengine .model .ResponseMetadata ;
46+ import com .volcengine .observability .debugger .SdkDebugLog ;
4547import com .volcengine .retryer .BackoffStrategy ;
4648import com .volcengine .retryer .DefaultRetryerSetting ;
4749import com .volcengine .retryer .RetryCondition ;
9799import java .util .regex .Matcher ;
98100import java .util .regex .Pattern ;
99101
102+ import static com .volcengine .observability .debugger .LogLevel .LOG_DEBUG_WITH_CONFIG ;
103+ import static com .volcengine .observability .debugger .SdkDebugLog .SDK_CORE_LOGGER ;
104+
100105public class ApiClient extends BaseClient {
101106 private final static String DefaultAuthentication = "volcengineSign" ;
102107
@@ -144,6 +149,7 @@ public class ApiClient extends BaseClient{
144149
145150 private String httpProxy ;
146151 private String httpsProxy ;
152+ private SdkConfigLog sdkConfigLog ;
147153
148154 /*
149155 * Constructor for ApiClient
@@ -152,6 +158,7 @@ public ApiClient() {
152158 ConnectionPool connectionPool = new ConnectionPool (maxIdleConns , keepAliveDurationMs );
153159 httpClient = new OkHttpClient ();
154160 httpClient .setConnectionPool (connectionPool );
161+ httpClient .interceptors ().add (new com .volcengine .interceptor .HttpLoggingInterceptor ());
155162 verifyingSsl = true ;
156163
157164 json = new JSON ();
@@ -464,21 +471,11 @@ public boolean isDebugging() {
464471
465472 /**
466473 * Enable/disable debugging for this API client.
467- *
474+ * @Deprecated
468475 * @param debugging To enable (true) or disable (false) debugging
469476 * @return ApiClient
470477 */
471478 public ApiClient setDebugging (boolean debugging ) {
472- if (debugging != this .debugging ) {
473- if (debugging ) {
474- loggingInterceptor = new HttpLoggingInterceptor ();
475- loggingInterceptor .setLevel (Level .BODY );
476- httpClient .interceptors ().add (loggingInterceptor );
477- } else {
478- httpClient .interceptors ().remove (loggingInterceptor );
479- loggingInterceptor = null ;
480- }
481- }
482479 this .debugging = debugging ;
483480 return this ;
484481 }
@@ -1091,6 +1088,7 @@ public <T> ApiResponse<T> execute(Call call, final Type returnType, boolean... i
10911088 responseInterceptorContext .setReturnType (returnType );
10921089 context .setResponseContext (responseInterceptorContext );
10931090 context .setApiClient (this );
1091+ logSdkConfig ();
10941092
10951093 int numMaxRetries = retryer .getNumMaxRetries ();
10961094 ApiException apiException ;
@@ -1144,10 +1142,12 @@ private ApiException handleApiResponseException(ApiException apiException) {
11441142
11451143 private boolean requestShouldRetry (ApiResponse apiResponse , int retryCount , ApiException lastException ) throws ApiException {
11461144 if (autoRetry && retryer .shouldRetry (apiResponse , retryCount , lastException )) {
1145+ SDK_CORE_LOGGER .debugRetry ("maxReryCout:{}, currentRetryCount:{}, backoffStrategy:{}" , retryer .getNumMaxRetries (), retryCount + 1 , retryer .getBackoffStrategy ().getClass ().getSimpleName ());
11471146 try {
11481147 long delay = retryer .getBackoffDelay (retryCount );
11491148 Thread .sleep (delay );
11501149 } catch (Exception e ) {
1150+ SDK_CORE_LOGGER .error (()->"Failed to getBackoffDelay or sleep" , e );
11511151 throw new ApiException (e );
11521152 }
11531153 return true ;
@@ -1188,6 +1188,7 @@ public <T> void executeAsync(Call call, final Type returnType, final ApiCallback
11881188 context .setResponseContext (responseInterceptorContext );
11891189
11901190 context .setApiClient (this );
1191+ logSdkConfig ();
11911192
11921193 final int maxRetries = retryer .getNumMaxRetries ();
11931194 final AtomicInteger retryCount = new AtomicInteger (0 );
@@ -2008,4 +2009,76 @@ public ApiClient setBackoffStrategy(BackoffStrategy backoffStrategy) throws ApiE
20082009 public OkHttpClient getOkHttpClient () {
20092010 return this .httpClient ;
20102011 }
2012+
2013+ /**
2014+ * 设置日志级别。
2015+ *
2016+ * <p>logLevel 的值应来自 {@link LogLevel} 枚举的 {@link LogLevel#mask()},
2017+ * 可以通过 {@link LogLevel#combine(LogLevel...)} 组合多个模式。</p>
2018+ *
2019+ * <p>常见用法示例:</p>
2020+ * <pre>{@code
2021+ * // 只启用请求日志
2022+ * logger.setLogLevel(LogLevel.LOG_DEBUG_WITH_REQUEST.mask());
2023+ *
2024+ * // 启用请求和响应日志
2025+ * logger.setLogLevel(LogLevel.combine(
2026+ * LogLevel.LOG_DEBUG_WITH_REQUEST,
2027+ * LogLevel.LOG_DEBUG_WITH_RESPONSE));
2028+ *
2029+ * // 启用所有调试日志
2030+ * logger.setLogLevel(LogLevel.LOG_DEBUG_ALL.mask());
2031+ * }</pre>
2032+ *
2033+ * @param logLevel 日志级别标志位,参考 {@link LogLevel}
2034+ */
2035+ public ApiClient setLogLevel (long logLevel ) {
2036+ SdkDebugLog .SDK_CORE_LOGGER .setLogLevel (logLevel );
2037+ return this ;
2038+ }
2039+
2040+ public long getLogLevel () {
2041+ return SdkDebugLog .SDK_CORE_LOGGER .getLogLevel ();
2042+ }
2043+
2044+ private void logSdkConfig () {
2045+ if (!SDK_CORE_LOGGER .isDebugEnabled () || !SDK_CORE_LOGGER .matches (LOG_DEBUG_WITH_CONFIG )){
2046+ return ;
2047+ }
2048+
2049+ if (this .sdkConfigLog != null ) {
2050+ this .sdkConfigLog .log ();
2051+ }
2052+ synchronized (this ){
2053+
2054+ if (this .sdkConfigLog != null ) {
2055+ this .sdkConfigLog .log ();
2056+ }
2057+
2058+ SdkConfigLog sdkConfigLog = new SdkConfigLog ();
2059+ sdkConfigLog .setMaxIdleConns (this .maxIdleConns );
2060+ sdkConfigLog .setKeepAliveDurationMs (this .keepAliveDurationMs );
2061+ sdkConfigLog .setDisableSSL (this .disableSSL );
2062+ sdkConfigLog .setVerifyingSsl (this .verifyingSsl );
2063+ sdkConfigLog .setHttpProxy (this .httpProxy );
2064+ sdkConfigLog .setHttpsProxy (this .httpsProxy );
2065+ sdkConfigLog .setConnectTimeout (this .getConnectTimeout ());
2066+ sdkConfigLog .setReadTimeout (this .getReadTimeout ());
2067+ sdkConfigLog .setWriteTimeout (this .getWriteTimeout ());
2068+ sdkConfigLog .setAutoRetry (this .autoRetry );
2069+ sdkConfigLog .setMinRetryDelayMs (this .getMinRetryDelayMs ());
2070+ sdkConfigLog .setMaxRetryDelayMs (this .getMaxRetryDelayMs ());
2071+ sdkConfigLog .setRetryCondition (this .getRetryCondition ());
2072+ sdkConfigLog .setBackoffStrategy (this .getBackoffStrategy ());
2073+ sdkConfigLog .setRetryErrorCode (this .getRetryErrorCodes ());
2074+ sdkConfigLog .setRegion (this .region );
2075+ sdkConfigLog .setEndpoint (this .endpoint );
2076+ sdkConfigLog .setUseDualStack (this .useDualStack );
2077+ sdkConfigLog .setCustomBootstrapRegion (this .customBootstrapRegion );
2078+ sdkConfigLog .setEndpointResolver (this .endpointResolver );
2079+ sdkConfigLog .log ();
2080+ this .sdkConfigLog = sdkConfigLog ;
2081+ }
2082+
2083+ }
20112084}
0 commit comments