Logging Try it

Basics

Building off the config store, Prefab logging allows you to dynamically control log levels at runtime for each class & method in your project.

When you call Services::Prefab.client.log.info the client will lookup the path of the caller_location and see what the closest matching log level is. If that level is higher than the log level you want, it will skip logging.

For example:

class DocumentationController < ApplicationController
  def logging
    Rails.logger.debug "debug level logging"
    Rails.logger.info "info level logging"
    Rails.logger.warn "warn level logging"
  end
end

will produce logging that looks like:

info level logs

We can then set the log level to debug for just this controller

log level UI

and we will instantly get debug level output

debug level logs

Rails Logger

The Prefab logger extends the basic Ruby logger and is perfectly suitable for use as a Rails logger. You can do this by setting the Rails.logger like this:

#config/initializers/prefab.rb
$prefab = Prefab::Client.new(shared_cache: Rails.cache,
                               logdev: $stdout)
Rails.logger = $prefab.log

#puma.rb
on_worker_boot do
  $prefab.config_client.start_streaming
end

The reason this is in the puma.rb is that GRPC does not like to be forked. See Issue #7951 for more details about GRPC and forking. The Prefab client uses GRPC so should be instantiated after processes fork.

You'll want to do a similar thing for other forking ruby servers like Unicorn by putting this in its after_fork.