Better Stack Ruby & Rails logging

Where do you want to collect your Ruby logs from?

Logging from Rails

Collect logs from your Rails application.

1. Install

Install Better Stack Rails Gem:

Add Logtail Gem
bundle add logtail-rails

2. Setup

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

 
module YourProject
  class Application < Rails::Application
    # ...
    # configuration of your project
    # ...

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

Setup filtering of sensitive information

To optionally filter sensitive parameters and/or HTTP headers, create a new initializer:

config/initializers/filter_logging.rb
# Be sure to restart your server when you modify 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
]

3. Start logging ๐ŸŽ‰

Use Rails logger as usual:

Send logs to Logtail
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 Logtail -> Live tail.

warning

Rails version 6.1.4.2 or higher is required.
Ruby version 2.7.0 or higher is required.

Logging from Ruby

Collect logs from your Ruby code.

1. Install

Install Better Stack Gem:

Add Logtail Gem
bundle add logtail

2. Setup

Set up Better Stack Ruby client:

Create Logtail 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 Logtail
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 Logtail
logger.close

You should see your logs in Logtail -> Live tail.

warning

Ruby version 2.7.0 or higher is required.

Need help?

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

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 Ruby
Logtail.with_context(user: { id: 123 }) do
    Rails.logger.info('User logged in.')
    Rails.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 logs sent to Better Stack

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

Rails 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
# 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.