Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Customize logger

This topic describes how to customize the log information about experiments 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.

The React SDK comes with a default Logger implementation. To configure the log level threshold, you can call setLogLevel.

import {
  setLogLevel,
} from '@optimizely/react-sdk'

// Set log level to debug
// Can be 'info', 'debug', 'warn', 'error'
setLogLevel('debug');

To turn off logging from the SDK, call setLogger with null:

import {
  setLogger,
} from '@optimizely/react-sdk'

// To turn off logging, call setLogger with null
setLogger(null);

For finer control over your SDK configuration in a production environment, pass in a custom logger for your Optimizely client. A custom logger is a function that takes an argument, the level, and the message. See the code example below to create and set a custom logger.

import {
  setLogLevel,
  setLogger,
} from '@optimizely/react-sdk'

// Set log level
setLogLevel('debug');

/**
 * customLogger
 * 
 * Example of a custom logger. A custom logger is a function that
 * takes two parameters (level, message) and logs to an appropriate place,
 * typically the console.
 *
 * @param {string} level indicating the level of the log message
 * @param {string} message indicating the log message
 *
 */
var customLogger = function(level, message) {
  var LOG_LEVEL = optimizelySDK.enums.LOG_LEVEL;
  switch (level) {
    case LOG_LEVEL.INFO:
      // INFO log message
      console.log(message);
      break;
    
    case LOG_LEVEL.DEBUG:
      // DEBUG log message
      console.log(message);
      break;

    case LOG_LEVEL.WARNING:
      // WARNING log message
      console.log(message);
      break;

    case LOG_LEVEL.ERROR:
      // ERROR log message
      console.log(message);
      break;
  }
}

// Set the custom logger
setLogger({
  log: customLogger,
});

Log levels

The table below lists the log levels for the JavaScript (React) SDK.

Log LevelExplanation
ERROREvents 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.
WARNINGEvents 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) are logged.
INFOEvents of significance (for example, activate started, activate succeeded, tracking started, and tracking succeeded) are logged. This is helpful in showing the lifecycle of an API call.
DEBUGAny 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.