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

Customize the JavaScript (Node) SDK logger

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

const { setLogLevel, setLogger, logging } = require('@optimizely/optimizely-sdk');

// Set log level to debug
// Can be 'info', 'debug', 'warn', 'error'

// To turn off logging, call setLogger with null



When using the default logger: in the latest version of Node SDK, you need to manually create the logger. Use the following line to create and set a logger:

// create logger

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.

const { setLogLevel, setLogger, enums } = require('@optimizely/optimizely-sdk');

// Set log level

 * 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;
      // DEBUG log message

      // WARNING log message

      // ERROR log message

// Set the custom logger
  log: customLogger,

Log levels

The table below lists the log levels for the JavaScript (Node) 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.