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 Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Customize the Python SDK logger

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

You can use our SimpleLogger implementation out of the box.

To improve your experience setting up the SDK and configuring your production environment, it is recommend that you pass in a logger for your Optimizely client. See the code example below.

import logging
from optimizely import logger
from optimizely import optimizely

# To set a log level choose one of the following:
# INFO: logging.INFO
# NOTSET: logging.NOTSET
# DEBUG: logging.DEBUG
# WARNING: logging.WARNING
# ERROR: logging.ERROR
# CRITICAL: logging.CRITICAL

# To define a minimum logging level pass it in during initialization.
# The example below shows a minimum logging level of INFO.
optimizely_client = optimizely.Optimizely(datafile, logger=logger.SimpleLogger(min_level=logging.INFO))

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.

import logging

def get_custom_logger(name, level=logging.INFO):
  """ Example of a custom logger.
  
    This function takes in two parameters: name and level and logs to console.
    The place to log in this case is defined by the handler which we set
    to logging.StreamHandler().
    
    Args:
      name: Name for the logger.
      level: Minimum level for messages to be logged
  """
  logger = logging.getLogger(name)
  logger.setLevel(level)

  if not logger.handlers:
    handler = logging.StreamHandler()
    formatter = logging.Formatter("Optimizely({}): %(levelname)s %(message)s".format(name))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

  return logger


# Here we initialize the SDK with the custom logger.
optimizely_client = optimizely.Optimizely(datafile, logger=get_custom_logger('my_optimizely_logger'))

Log levels

The table below lists the log levels for the Python SDK.

Log LevelExplanation
logging.CRITICALEvents that cause the app to crash are logged.
logging.ERROREvents that prevent feature flags from functioning correctly (for example, invalid datafile in initialization and invalid flag keys) are logged. The user can take action to correct.
logging.WARNINGEvents that don't prevent feature flags from functioning correctly, but can have unexpected outcomes (for example, future API deprecation, logger or error handler are not set properly, and nil values from getters) are logged.
logging.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.
logging.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 flag rule) are logged.
logging.NOTSETLevel set when a logger is created. Causes all messages to be processed when the logger is the root logger (or delegation to the parent when the logger is a non-root logger).

See also

Loggers return:

  • SDK lifecycle
  • flag decision messages

You can also view flag decision messages in the Reasons field of returned decision objects. For more information, see OptimizelyDecision

Source files

The language/platform source files containing the implementation for Python is optimizely.py.