Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Customize the JavaScript SDK logger v6+

How to customize the log information about experiments coming from the Optimizely Feature Experimentation JavaScript SDK versions 6 and above to help with debugging.

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

Minimum SDK version

v6.0.0+

For versions 5.3.5 and below, see JavaScript (Browser) SDK or JavaScript (Node) SDK. See the SDK compatibility matrix documentation for a list of current SDK releases and the features they support.

Description

To enable logging for the Javascript SDK, create a logger using the createLoggerfactory function and pass the logger to createInstance.

import {
  createInstance,
  createPollingProjectConfigManager,
  createLogger,
  LogLevel,
  INFO
} from "@optimizely/optimizely-sdk";

const SDK_KEY = "YOUR_SDK_KEY";

const pollingConfigManager = createPollingProjectConfigManager({
  sdkKey: SDK_KEY,
});

const customLogHandler = {
  log: (level, message) => {
    switch (level) {
      case LogLevel.Info:
        // INFO log message
        console.info(message);
        break;
      
      case LogLevel.Debug:
        // DEBUG log message
        console.debug(message);
        break;

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

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

const logger = createLogger({
  level: INFO,
});

const loggerWithCustomHandler = createLogger({
  level: INFO,
  logHandler: customLogHandler,  
})

const optimizely = createInstance({
  projectConfigManager: pollingConfigManager,
  logger
});

To customize the logger instance, you could use following configuration:

ParameterTypeDescription
levelOpaqueLevelPresetLevel preset that can be used directly to create logger instance. Level presets can be imported from the package root.
logHandler
optional
LogHandlerAn interface that implements the log method. A custom LogHandlercan be used to modify how log messages are handled (for example, storing in a file or sending to an external system). By default, a console log handler is used which prints the log message to the console.

Log level presets

The following table lists the log level presets available:

Log LevelExplanation
ERROREvents that prevent feature flags or experiments from functioning correctly (for example, invalid datafile in initialization and invalid feature keys) are logged. You can take action to correct.
WARNEvents that do not 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.
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.
DEBUGAny information related to errors that can help you debug the issue (for example, the feature flag is not running, user is not included in the rollout) are logged.

They can be imported as follows:

import {
	DEBUG,
  INFO,
  WARN,
  ERROR
} from "@optimizely/optimizely-sdk";