All Products
Document Center

OpenSearch:Demo code for exporting SDK logs

Last Updated:Sep 09, 2021

Demo code for exporting logs during debugging

Step 1: Add OpenSearch SDK for Java as a dependency in the pom.xml file if you have not done so. In addition, add the following dependency:


Step 2: Save the following code as the log4j.xml file and store the file in the directory specified by classpath.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%t] %c{1} - %m%n" />
    <appender name="logfile" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="log/sdk.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %p [%c] - %m%n" />
    <logger name="com.aliyun.opensearch">
        <level value="debug" />
    <logger name="org.apache.thrift">
        <level value="error" />
        <level value="info" />
        <appender-ref ref="console" />
        <appender-ref ref="logfile" />

step 3: Optional. In the log4j.xml file, change the destination file to which logs are to be exported.

 <param name="File" value="log/sdk.log" />

Set the value parameter to the destination path.