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 createLogger
factory 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 -
Parameter | Type | Description |
---|---|---|
level | OpaqueLevelPreset | Level presets that can be used directly to create logger instance |
logHandler optional | LogHandler | An interface that implements the log method. A custom LogHandler can 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 Level | Explanation |
---|---|
ErrorLog | Events 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. |
WarnLog | Events 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. |
InfoLog | 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. |
DebugLog | Any 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. |
Updated about 22 hours ago