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 (Browser) SDK to help with debugging.

❗️

Warning

This content covers the Feature Experimentation JavaScript (Browser) SDK v6 features currently in pre-production testing and is subject to change before release

For the latest released version, see JavaScript (Browser) SDK.

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+

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,
  InfoLog,
} from "@optimizely/optimizely-sdk";

const SDK_KEY = "YOUR_SDK_KEY";

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

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

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

To customize the logger instance, you could use following config -

ParameterTypeDescription
levelOpaqueLevelPresetLevel presets that can be used directly to create logger instance
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 for the JavaScript SDK:

Log LevelExplanation
ErrorLogEvents 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.
WarnLogEvents 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.
InfoLogEvents 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.
DebugLogAny 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.