Customize the JavaScript SDK v6+ logger
How to customize the log information about experiments coming from the Optimizely Feature Experimentation JavaScript SDK v6+ to help with debugging.
The logger captures details about your experiments, making it easier for you to debug them. You can customize where the log information is stored and choose the types of data you want to track.
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 createLogger
factory 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:
Parameter | Type | Description |
---|---|---|
| OpaqueLevelPreset | Level preset that can be used directly to create logger instance. Level presets can be imported from the package root. |
| LogHandler | An interface that implements the log method. A custom |
Log level presets
The following table lists the log level presets for the JavaScript SDK v6+.
Log Level | Explanation |
---|---|
ERROR | Logs events that prevent feature flags from functioning correctly, such as an invalid datafile during initialization or incorrect feature keys. These issues require user intervention to fix. |
WARN | Logs events that do not prevent feature flags from functioning correctly but may lead to unexpected outcomes, like future API deprecation, improper logger or error handler settings, and nil values from getters. |
INFO | Logs significant events, such as when a decision or tracking starts and succeeds, to illustrate the lifecycle of an API call. |
DEBUG | Logs additional information related to errors that assist Optimizely in debugging, like when a feature flag is not running or a user is not included in a rollout. |
They can be imported with the following code sample:
import {
DEBUG,
INFO,
WARN,
ERROR
} from "@optimizely/optimizely-sdk";
Updated 5 days ago