Help Center/ Object Storage Service/ SDK Reference/ Java/ FAQs (SDK for Java)/ How Do I Configure OkhttpEventListener Logs in Java SDK to Print Detailed Logs of Each Phase? (SDK for Java)
Updated on 2026-08-14 GMT+08:00

How Do I Configure OkhttpEventListener Logs in Java SDK to Print Detailed Logs of Each Phase? (SDK for Java)

Principle

You can use the EventListener of OkHttp to collect statistics on the time consumed in each phase of an HTTP request. This function is supported only in Java SDK 3.24.9 and later versions. Enabling OkhttpEventListener logs consumes extra performance. Therefore, you are advised to enable this function only during debugging.

Sample Code

import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.exception.ObsException;
import com.obs.services.internal.utils.OkhttpCallProfiler;
import java.io.ByteArrayInputStream;
public class PutObject001 {
    public static void main(String[] args) {
        // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
        // Obtain an AK/SK pair on the management console.
        String ak = System.getenv("ACCESS_KEY_ID");
        String sk = System.getenv("SECRET_ACCESS_KEY_ID");
        // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage.
        // Obtain an AK/SK pair and a security token using environment variables or import them in other ways. If you use a temporary AK/SK pair and a security token to make an API call, uncomment the following line.
        // String securityToken = System.getenv("SECURITY_TOKEN");
        // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
        String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";
        // Obtain an endpoint using environment variables or import it in other ways.
        //String endPoint = System.getenv("ENDPOINT");
        ObsConfiguration obsConfiguration = new ObsConfiguration();
        obsConfiguration.setEndPoint(endPoint);
        // Print statistical logs of an OkHttp call to System.out::println. You can replace it with other functions.
        // obsConfiguration.setEventListenerFactory(call -> new OkhttpCallProfiler(System.out::println));
        // Print statistical logs of an OkHttp call to the SDK's Log4j logs.
        obsConfiguration.setEventListenerFactory(call -> new OkhttpCallProfiler());
        // Create an ObsClient instance.
        try (ObsClient obsClient = new ObsClient(ak, sk, obsConfiguration)) {
            // Upload a string as a byte array.
            String content = "Hello OBS";
            String objKey = "objectkey.webp";
            String bucket = "examplebucket";
            obsClient.putObject(bucket, objKey, new ByteArrayInputStream(content.getBytes()));
            System.out.println(obsClient.getObjectMetadata(bucket,objKey).getContentType());
            System.out.println("putObject successfully");
        } catch (ObsException e) {
            System.out.println("putObject failed");
            // Request failed. Print the HTTP status code.
            System.out.println("HTTP Code:" + e.getResponseCode());
            // Request failed. Print the server-side error code.
            System.out.println("Error Code:" + e.getErrorCode());
            // Request failed. Print the error details.
            System.out.println("Error Message:" + e.getErrorMessage());
            // Request failed. Print the request ID.
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("putObject failed");
            // Print other error information.
            e.printStackTrace();
        }
    }
}