Better Stack Ruby & Rails logging

Where do you want to collect your Ruby logs from?

Logging from Rails

Collect logs from your Rails application including your Sidekiq jobs.

1. Install

Install Better Stack Rails Gem :

Add Better Stack Rails Gem
bundle add logtail-rails

2. Setup

Set up Better Stack Rails Gem in config/application.rb:

config/application.rb
module YourProject
  class Application < Rails::Application
    # ...
    # configuration of your project
    # ...

    config.logger = Logtail::Logger.create_default_logger("$SOURCE_TOKEN")
  end
end

3. Start logging πŸŽ‰

Use Rails logger as usual:

Send logs to Better Stack
Rails.logger.error("Something bad happened.")
Rails.logger.warn("Log message wih structured logging.", {
    item: "Orange Soda",
    price: 100.00
})

You should see your logs in Better Stack Logs β†’ Live tail .

Check out the Ruby on Rails dashboard .

Rails version 5.0 or higher is required.
Ruby version 2.5 or higher is required.

Logging from Rack

Collect logs from any Rack-based framework such as Sinatra , Hanami , Padrino , or Roda .

1. Install

Install Better Stack Rack Gem :

Add Better Stack Rack Gem
bundle add logtail-rack

2. Setup

Set up Better Stack Rack Gem in your config.ru:

config.ru
require 'logtail-rack'

# Initialization of logging middlewares (you don't have to use all)
use Logtail::Integrations::Rack::HTTPContext
use Logtail::Integrations::Rack::HTTPEvents
use Logtail::Integrations::Rack::ErrorEvent
use Logtail::Integrations::Rack::UserContext
use Logtail::Integrations::Rack::SessionContext

# Logger instance initialization
http_io_device = Logtail::LogDevices::HTTP.new("$SOURCE_TOKEN")
logger = Logtail::Logger.new(http_io_device)
Logtail::Config.instance.logger = logger

# Here is your application initialization
run ...

3. Start logging πŸŽ‰

Use the instantiated logger instance to send logs to Better Stack:

Send logs to Better Stack
logger = Logtail::Config.instance.logger

logger.info("I am using Better Stack! πŸš€")

# You can also provide additional information when logging
logger.debug("Logging structured data...",
  name: {
    first: "John",
    last: "Smith"
  },
  id: 123456
)

You should see your logs in Better Stack Logs β†’ Live tail .

Rack version 1.2 or higher is required.
Ruby version 2.3 or higher is required.

Logging from Ruby

Collect logs from your Ruby code.

1. Install

Install Better Stack Gem :

Add Better Stack Gem
bundle add logtail

2. Setup

Set up Better Stack Ruby client:

Create Better Stack logger
require "logtail"

http_device = Logtail::LogDevices::HTTP.new("$SOURCE_TOKEN")
logger = Logtail::Logger.new(http_device)

3. Start logging πŸŽ‰

Use Ruby logger as usual:

Send logs to Better Stack
logger.error("Something bad happened.")
logger.warn("Log message with structured logging.", {
    item: "Orange Soda",
    price: 100.00
})

# Close logger to ensure that all logs are sent to Better Stack
logger.close

You should see your logs in Better Stack Logs β†’ Live tail .

Ruby version 2.3.0 or higher is required.

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! πŸ™

Example projects

You can take a look at example projects on Github:

Additional information

Interested in learning more about log levels in Rails?
Check out our Intro guide to Ruby on Rails logging.

Enrich log context

Add custom data to multiple log lines using with block:

Rails Rack Ruby
Logtail.with_context(user: { id: 123 }) do
    Rails.logger.info('User logged in.')
    Rails.logger.info('User bought a gem.')
end
logger = Logtail::Config.instance.logger
Logtail.with_context(user: { id: 123 }) do
    logger.info('User logged in.')
    logger.info('User bought a gem.')
end
Logtail.with_context(user: { id: 123 }) do
    logger.info('User logged in.')
    logger.info('User bought a gem.')
end

Filter sensitive information

In Rails or Rack, you can filter sensitive parameters and HTTP headers:

Rails Rack
# config/initializers/filter_logging.rb
# Be sure to restart your server when you create this file.

# Configure parameters to be filtered from the log file.
# Use this to limit dissemination of sensitive information.
# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.
Rails.application.config.filter_parameters += [
  :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]

# Configure HTTP headers to be filtered from the log.
# Use this to limit dissemination of sensitive information.
Logtail::Integrations::Rack::HTTPEvents.http_header_filters = [
  :authorization, :proxy_authorization, :cookie
]
# config.ru
# Be sure to restart your server when you modify this file.

# Configure HTTP headers to be filtered from the log.
# Use this to limit dissemination of sensitive information.
Logtail::Integrations::Rack::HTTPEvents.http_header_filters = [
  :authorization, :proxy_authorization, :cookie
]

Filter logs sent to Better Stack

You can prevent logger from sending certain logs to Better Stack:

Rails Rack Ruby
# in your config/application.rb
config.logtail.filter_sent_to_better_stack do |log_entry|
    log_entry.context_snapshot[:http][:path].start_with?('/_')
end
# in your config.ru
Logtail.config.filter_sent_to_better_stack do |log_entry|
    log_entry.message.include?("IGNORE")
end
# anywhere before sending the logs
Logtail.config.filter_sent_to_better_stack do |log_entry|
    log_entry.message.include?("IGNORE")
end

See Logtail::LogEntry for available attributes of the block parameter.