HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Customize logger

This topic describes how to customize the log information about experiments coming from the Optimizely Java SDK to help with debugging.

The logger logs information about your experiments to help you with debugging. You can customize where log information is sent and what kind of information is tracked.

In the Java SDK, logging functionality is not enabled by default. To improve your experience setting up the SDK and configuring your production environment, we recommend that you include a SLF4J implementation in your dependencies. For the Java SDK, we require the use of an SLF4J implementation.

To improve your experience setting up the SDK and configuring your production environment, we recommend that you pass in a logger for your Optimizely client. See the code example below.

❗️

log4j Vulnerability Advisory

Due to a recently announced security exploit in the Log4J library, we recommend upgrading to version 2.15.0 or higher as soon as possible if you are using it alongside the SDK. View Apache's documentation on the Log4j vulnerability for more information.

// Provide a SLF4j binding like logback or log4j

compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
compile group: 'log4j', name: 'log4j', version: '2.15.0'
//For formatting: under slf4j.properties
  
# Root logger option
log4j.rootLogger=DEBUG, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d

{yyyy-MM-dd HH:mm:ss}
%-5p %c

{1}
:%L - %m%n

For finer control over logging, for example to control the logging level per package or to control the logging destination, we recommend you use logback.

implementation 'ch.qos.logback:logback-classic:1.1.7'

Save the logback.xml file in your resources directory. Below is a simple example of capturing Optimizely log levels and piping them somewhere else.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="MEMORY" class="com.optimizely.intellij.plugin.utils.LogAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <logger name="com.optimizely" level="debug" additivity="false">
        <appender-ref ref="MEMORY"/>
    </logger>

    <root level="error">
        <appender-ref ref="CONSOLE"/>
    </root>

The preceding example pipes all debug logs to error messages. These are then sent to a different log appender.

You can also set logging levels for any Optimizely package, giving you control over logs granularity. In the following example, we simply set the log level for the highest-level package, com.optimizely:

<logger name="com.optimizely" level="debug" />

Finally, the following general example show you how to write a custom appender rather than using one of the many default appenders:

public class LogAppender extends AppenderBase<ILoggingEvent> {
    int counter = 0;
    public static AtomicBoolean captureLogging = new AtomicBoolean(false);
    public static List<String> logs = Collections.synchronizedList(new ArrayList<String>());

    PatternLayoutEncoder encoder;

    @Override
    public void start() {
        if (this.encoder == null) {
            addError("No encoder set for the appender named ["+ name +"].");
            return;
        }

        try {
            encoder.init(System.out);
        } catch (IOException e) {
        }
        super.start();
    }

    public static void clearLogs() {
        synchronized (logs) {
            logs.clear();
        }
    }

    public void append(ILoggingEvent event) {
        // output the events as formatted by our layout
        try {
            this.encoder.doEncode(event);
            if (captureLogging.get()) {
                synchronized (logs) {
                    logs.add(event.getFormattedMessage());
                }
            }
        } catch (IOException e) {
        }

        // prepare for next event
        counter++;
    }

    public PatternLayoutEncoder getEncoder() {
        return encoder;
    }

    public void setEncoder(PatternLayoutEncoder encoder) {
        this.encoder = encoder;
    }
}

Log levels

The table below lists the log levels for the Java SDK.

Log Level

Explanation

ERROR

Events that prevent feature flags from functioning correctly (for example, invalid datafile in initialization and invalid feature keys) are logged. The user can take action to correct.

WARNING

Events that don't prevent feature flags from functioning correctly, but can have unexpected outcomes (for example, future API deprecation, logger or error handler are not set properly, and nil values from getters) are logged.

INFO

Events of significance (for example, decision started, decision succeeded, tracking started, and tracking succeeded) are logged. This is helpful in showing the lifecycle of an API call.

DEBUG

Any information related to errors that can help us debug the issue (for example, the feature flag is not running, user is not included in the rollout) are logged.


Did this page help you?