Where do you want to collect your Ruby logs from?
Collect logs from your Rails application, including your Sidekiq jobs. This integration also supports Structured Events in Rails 8.1 or higher for rich, queryable event logging.
Install Better Stack Rails Gem:
bundle add logtail-rails
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",
ingesting_host: "$INGESTING_HOST",
)
end
end
Use Rails logger as usual:
Rails.logger.error("Something bad happened.")
Rails.logger.warn("Log message with structured logging.", {
item: "Orange Soda",
price: 100.00
})
# Send a structured event (in Rails 8.1 or higher)
Rails.event.notify("user.signed_up", email: "user@example.com")
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.
Collect logs from any Rack-based framework such as Sinatra, Hanami, Padrino, or Roda.
Install Better Stack Rack Gem:
bundle add logtail-rack
Set up Better Stack Rack Gem in your 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",
ingesting_host: "$INGESTING_HOST",
)
logger = Logtail::Logger.new(http_io_device)
Logtail::Config.instance.logger = logger
# Here is your application initialization
run ...
Use the instantiated logger instance to 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.
Collect logs from your Ruby code.
Install Better Stack Gem:
bundle add logtail
Set up Better Stack Ruby client:
require "logtail"
http_io_device = Logtail::LogDevices::HTTP.new(
"$SOURCE_TOKEN",
ingesting_host: "$INGESTING_HOST",
)
logger = Logtail::Logger.new(http_io_device)
Use Ruby logger as usual:
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 β Live tail.
Ruby version 2.3.0 or higher is required.
Please let us know at hello@betterstack.com.
We're happy to help! π
You can take a look at example projects on Github:
Interested in learning more about log levels in Rails?
Check out our Intro guide to Ruby on Rails logging.
Add custom data to multiple log lines using with block:
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
In Rails or Rack, you can filter sensitive parameters and HTTP headers:
# 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
]
You can prevent logger from sending certain logs to Better Stack:
# 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.
Starting with Rails 8.1, you can use the new Structured Events system to send structured data to Better Stack. The logtail-rails gem automatically subscribes to these events and logs them as structured log entries in case you've used Logtail::Logger.create_default_logger to initialize.
This is particularly useful for tracking key business events or application-specific actions with rich, queryable data, without having to format them into a log message.
# Notify about a simple event with a payload
Rails.event.notify("user.signed_up", email: "user@example.com")
# You can add context that will be included in all subsequent events
Rails.event.set_context(request_id: "abc-123", shop_id: 456)
# Tag specific events for easier filtering
Rails.event.tagged("api", "billing") do
Rails.event.notify("payment.processed", amount: 9.99, currency: "USD")
end
# If you haven't used Logtail::Logger.create_default_logger
# the logger needs to be subscribed manually to the events
Rails.event.subscribe(
Logtail::Integrations::Rails::EventLogSubscriber.new(logger)
)
# The event subscriber can have the log level changed
Logtail::Integrations::Rails::EventLogSubscriber.log_level = :debug
# The event subscriber can be disabled
Logtail::Integrations::Rails::EventLogSubscriber.enabled = false
When you use Rails.event.notify, the logtail-rails gem captures the event name, payload, context, tags, and source location, and sends it all as a structured log to Better Stack.
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.