SDK log outputs

更新时间:
复制 MD 格式

When debugging an OpenSearch Java application, you can export SDK logs to the console and a rolling log file by adding a logging binding dependency and creating a log4j configuration file. This lets you inspect SDK activity and diagnose integration issues.

Prerequisites

Before you begin, ensure that you have added the OpenSearch SDK for Java dependency to your pom.xml.

Add the logging dependency

In your pom.xml, add the following dependency alongside the OpenSearch SDK for Java dependency:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.12</version>
</dependency>

Configure log4j

Save the following content as log4j.xml in the directory specified by your 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>

This configuration routes SDK logs to both the console (System.out) and a rolling log file (log/sdk.log). The logger named com.aliyun.opensearch corresponds to the Java package used by the OpenSearch SDK — setting it to debug captures detailed SDK activity. The org.apache.thrift logger is set to error to suppress verbose internal Thrift output.

Change the log output path (optional)

To write logs to a different file, update the File parameter in the DailyRollingFileAppender section:

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

Set value to the destination path, for example logs/opensearch-debug.log.