Better Stack Ruby & Rails
Where do you want to collect your Ruby logs from?
Logging from Rails
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.
1. Install
Install Better Stack Rails Gem:
2. Setup
Set up Better Stack Rails Gem in config/application.rb:
Keep development logs local?
Set config.logtail.enabled = false in the environment file, for example development.rb. Rails then logs to its log file as usual and nothing is sent to Better Stack. Needs logtail-rails 0.2.15 or newer.
3. Start logging 🎉
Use Rails logger as usual:
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:
2. Setup
Set up Better Stack Rack Gem in your config.ru:
3. Start logging 🎉
Use the instantiated logger instance to send logs to Better Stack:
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:
2. Setup
Set up Better Stack Ruby client:
3. Start logging 🎉
Use Ruby logger as usual:
You should see your logs in Better Stack → Live tail.
Ruby version 2.3.0 or higher is required.
N+1 queries
Bullet detects N+1 queries, unused eager loading, and missing counter caches in your Rails app. We'll log each detection as a structured event, tagged with the file and line that triggered it.
1. Install
Add the Bullet gem:
2. Configure
Enable Bullet in config/environments/production.rb and register a notifier that emits one structured event per detection through Rails.logger:
BetterStackBulletNotifier is a regular UniformNotifier — the same library Bullet uses for its other output channels. It runs alongside any Bullet notifiers you already have enabled (alert, console, the in-browser footer) without affecting them.
3. Trigger a detection
Trigger an N+1 in your app. Open Live tail and you'll see one structured event per detection:
Tracing from Rails
Trace every request and Sidekiq job in your Rails app. We turn the traces into request and job metrics automatically.
1. Install
Add the OpenTelemetry gems:
2. Setup
Set up tracing in config/initializers/opentelemetry.rb. This initializer covers requests, database queries, and both enqueueing and processing jobs.
3. Start tracing
Send a few requests to your app and run any job. Within a couple of minutes, the data will become visible in the Tracing for Rails dashboard and in the Sidekiq section of the Ruby on Rails dashboard
Request queueing
queue_time is how long each request waited, in seconds, between your proxy accepting it and Rails starting to work on it. Request durations don't include this wait, so queue_time is the metric that shows requests piling up when all your workers are busy. Chart it in any dashboard.
We compute it from your traces. It needs the proxy in front of your app to set the X-Request-Start header:
- Heroku, Kamal 2, and Thruster 0.1.15 or newer set it already.
- nginx needs one more
proxy_set_headerline, next to the otherproxy_set_headerlines that apply to your app:
- Other proxies and load balancers need to set
X-Request-Startto the time they received the request, as a Unix timestamp in seconds, milliseconds, or microseconds.
Without the header, queue_time stays empty.
Tracing with the Better Stack collector?
Its eBPF traces don't include request queueing, so queue_time stays empty for them. Use the OpenTelemetry setup above to measure it.
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:
Add the user to every line of a request
In Rails, Devise, any other Warden-based authentication, and Clearance users are added automatically. With other authentication, return the user from the Rack environment:
The hash is logged as context.user on every line of the request, including the Started and Completed lines, which a with_context block in a controller can't reach. For other per-request context, use a Rack middleware inserted before Logtail::Integrations::Rack::HTTPEvents. See the example project.
Filter sensitive information
The Authorization, Proxy-Authorization, Cookie and Set-Cookie headers are logged as [FILTERED] out of the box, starting with logtail-rack 0.2.7.
Starting with logtail-rack 0.2.9, secrets in query strings and in Referer and Location URLs are logged as [FILTERED] too. This covers every query parameter whose name contains passw, secret, token, _key, crypt, salt, certificate, otp, ssn, cvv or cvc. In Rails, logtail-rails 0.3.0 and newer filter these URLs with your app's filter_parameters instead.
In Rails or Rack, you can filter more headers and sensitive parameters:
To log every header or query parameter unfiltered, set http_header_filters or query_string_filters to an empty array.
Filter logs sent to Better Stack
You can prevent logger from sending certain logs to Better Stack:
See Logtail::LogEntry for available attributes of the block parameter.
Structured Events
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.
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.
Including raw SQL in OpenTelemetry traces
If you're using OpenTelemetry instrumentation for PostgreSQL (pg gem), by default it sanitizes SQL queries in traces, replacing values with ?. This is done to prevent leaking sensitive data.
However, seeing the full query with values can be very helpful for debugging. You can configure the instrumentation to include the raw SQL statement.
This allows you to see the exact queries being run in your application traces, providing more context for performance analysis and troubleshooting.
To disable query sanitization for the pg instrumentation, set the db_statement option to :include in the use_all call of your OpenTelemetry initializer. This is typically done in config/initializers/opentelemetry.rb for a Rails application.
Including raw SQL queries in your traces might expose sensitive information like user IDs, e-mails, or other PII. Ensure you have other measures in place to protect this data or that you are comfortable with this information appearing in your traces.