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 ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Customize the Ruby SDK logger

Customize log information from the Optimizely Feature Experimentation Ruby 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.

The Ruby SDK does not have a logger enabled by default. You need to pass in a logger to the SDK to have basic logging functionality. The Ruby SDK provides a simple logger implementation that you can instantiate and pass in.

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.

# Require the logger library in to get the log levels
require 'logger'
require 'optimizely'

# Instantiate an Optimizely logger with the default log level Logger::INFO
logger = Optimizely::SimpleLogger.new 

# You can also instantiate with a custom log level
logger = Optimizely::SimpleLogger.new(Logger::DEBUG)

# Instantiate the Optimizely client with our logger
optimizely_client = Optimizely::Project.new(
  datafile: datafile,
  logger: logger
)

Use your own logger

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 receives two arguments: the log level and the message. See the following code sample below to create and set a custom logger.

# Define your own custom logger that inherits from the Optimizely base logger
class CustomLogger < Optimizely::BaseLogger
  def initialize
    super()
    @logger = Logger.new($stdout)
  end

  def log(level, message)
    # You can handle the message here in any way you'd like
    custom_message = "Custom logged: #{message}"
    @logger.add(level, custom_message)
  end
end

Log levels

The following list shows the log levels for the Ruby SDK.

  • Optimizely::SimpleLogger.new(Logger::CRITICAL) – Logs events that cause the app to crash.
  • Optimizely::SimpleLogger.new(Logger::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.
  • Optimizely::SimpleLogger.new(Logger::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.
  • Optimizely::SimpleLogger.new(Logger::INFO) – Logs key events to illustrate the lifecycle of an API call, such as when a decision or tracking starts and succeeds.
  • Optimizely::SimpleLogger.new(Logger::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.