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:

Set up
rake logtail:install source_token=$SOURCE_TOKEN

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