Extending logging functionality

If you have already adopted some method for logging events, you may want to use that method to log experiment-related events, such as exposures and experiment-specific outcomes. You can do this by extending the Experiment abstract class and overriding the log method.

Overriding the log method

To log exposures using your existing logging system, just override the log method when extending the Experiment abstract class. For example, if you write to your logs with MySQLLogger, then you might reimplement log as follows.


  def log(self, data):
    # self.MySQLLogger may be defined here or in configure_logger()
    self.MySQLLogger.log(data)

Writing a log configuration method

Since all code in log() gets called every time a log event is triggered, you may wish to set up a connection to your logging infrastructure in advance, immediately before the first time you need to log the data. To do this, you can override the configure_logger() method.

In SimpleExperiment configure_logger() actually sets a class variable, once per experiment name, so that a connection to the logging module is only made once per interpreter session.

Adding caching

By default, each instance of an Experiment class will log auto-exposure once per instance. But this can still result in a lot of writing when there are many instances created for a particular unit’s assignment (for example, if an experiment parameter is retrieved each time a person loads the home page). So you may want to add some caching (e.g., with memcache) to prevent unnecessary logging. This can be done by overriding the previously_logged() method.

More information

Additional documentation for the Experiment class and SimpleExperiment class can be found in the source code.