Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Customize the JavaScript (Browser) SDK logger

How to customize the log information about experiments coming from the Optimizely Feature Experimentation JavaScript (Browser) 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.

The JavaScript SDK comes with a default Logger implementation set to the ERROR log level . To configure the log level threshold, you can call setLogLevel on the SDK.

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

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

// 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, enums } from '@optimizely/optimizely-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
 *
 */
const customLogger = (level, message) => {
  var LOG_LEVEL = enums.LOG_LEVEL;
  switch (level) {
    case LOG_LEVEL.INFO:
      // INFO log message
      console.info(message);
      break;
    
    case LOG_LEVEL.DEBUG:
      // DEBUG log message
      console.debug(message);
      break;

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

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


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

Log levels

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

Log LevelExplanation
optimizelySDK.enums.LOG_LEVEL.ERROREvents that prevent feature flags or experiments from functioning correctly (for example, invalid datafile in initialization and invalid feature keys) are logged. The user can take action to correct.
optimizelySDK.enums.LOG_LEVEL.WARNINGEvents that don't prevent feature flags or experiments from functioning correctly, but can have unexpected outcomes (for example, future API deprecation, logger or error handler are not set properly) are logged.
optimizelySDK.enums.LOG_LEVEL.INFOEvents 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.
optimizelySDK.enums.LOG_LEVEL.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.