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.

What's that mean? A video is worth a thousand words here.

video embed of using dynamic logging in ruby on rails

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

Using with LogRage

The Prefab logger works great in conjunction with lograge. Lograge tames Rails's default behavior, and prefab allows dynamic level configuration.

# gem 'lograge'
# gem 'prefab-cloud-ruby'

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

Rails.application.configure do
  config.lograge.enabled = true
  config.log_level = :debug
end

Rails.logger = $prefab.log