All Products
Search
Document Center

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:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.12</version>
</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="http://jakarta.apache.org/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" />
        </layout>
    </appender>
    <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" />
        </layout>
    </appender>
    <logger name="com.aliyun.opensearch">
        <level value="debug" />
    </logger>
    <logger name="org.apache.thrift">
        <level value="error" />
    </logger>
    <root>
        <level value="info" />
        <appender-ref ref="console" />
        <appender-ref ref="logfile" />
    </root>
</log4j:configuration>

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.