Customize the Python SDK logger
Customize log information from the Optimizely Feature Experimentation Python SDK for debugging experiments.
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.
You can use Optimizely's SimpleLogger
implementation immediately.
To improve your experience setting up the SDK and configuring your production environment, pass in a logger for your Optimizely client. See the following code sample.
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))
Pass in a custom logger for your Optimizely client for finer control over your SDK configuration in a production environment. A custom logger is a function that takes an argument, the level, and the message. See the following code sample 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 following list shows the log levels for the Python SDK.
- logging.CRITICAL – Logs events that cause the app to crash.
- logging.ERROR – Logs events that prevent feature flags from working properly, such as an invalid data file during initialization or incorrect feature keys. These issues require user intervention to fix.
- logging.WARN – Logs events that do not prevent feature flags from working properly but may cause unexpected results, such as future API deprecation, improper logger or error handler settings, and nil values from getters.
- logging.INFO – Logs key events to illustrate the lifecycle of an API call, such as when a decision or tracking starts and succeeds.
- logging.DEBUG – Logs extra information related to errors that aid Optimizely in debugging, like when a feature flag is not running or a user is not included in a rollout.
- logging.NOTSET – Level set when a logger is created to allow message processing if the logger is the root logger, or to delegate processing to the parent if it 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 or platform source files containing the implementation for Python is optimizely.py.
Updated 11 days ago