---
title: "Logs"
description: "Structured logs allow you to send, view and query logs sent from your Rails applications within Sentry."
url: https://docs.sentry.io/platforms/ruby/guides/rails/logs/
---

# Set Up Logs | Sentry for Rails

With Sentry Structured Logs, you can send text-based log information from your Rails applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.

## [Requirements](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#requirements)

Logs for Rails are supported in Sentry Rails SDK version `5.27.0` and above.

```bash
gem install sentry-rails
```

Or add it to your Gemfile:

```ruby
gem "sentry-rails"
```

## [Setup](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#setup)

To enable logging, initialize the SDK with `enable_logs` set to `true`:

```ruby
Sentry.init do |config|
  config.dsn = "___PUBLIC_DSN___"
  config.enable_logs = true
end
```

This enables:

* The `Sentry.logger` API for explicit logging
* Automatic structured logging of Rails framework events (ActiveRecord queries, controller actions, etc.)

See the [Usage](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#usage) section for examples and [Options](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#options) to customize which events are captured.

## [Usage](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#usage)

Use the `Sentry.logger` API to send logs to Sentry:

```ruby
Sentry.logger.info("User logged in successfully")
Sentry.logger.error("Payment processing failed")
```

You can include additional context data as attributes:

```ruby
Sentry.logger.error(
  "Failed to process payment. Order: %{order_id}",
  order_id: "or_2342",
  amount: 99.99
)
```

Additional attributes like `amount` are sent as searchable metadata in Sentry's Logs UI.

See [Default Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#default-attributes) for the complete list of metadata and [Options](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#options) to customize which events are captured.

## [Options](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#options)

### [before\_send\_log](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#before_send_log)

To filter logs before they are sent to Sentry, use the `before_send_log` callback. Return `nil` to skip a log, or return the log object to send it.

```ruby
Sentry.init do |config|
  config.dsn = "___PUBLIC_DSN___"
  config.enable_logs = true

  config.before_send_log = lambda do |log|
    # Skip info logs
    return if log.level == :info

    log
  end
end
```

### [Structured Logging Subscribers](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#structured-logging-subscribers)

Structured logging is enabled by default when you set `enable_logs = true`. You can customize which Rails events are captured.

**Default subscribers (enabled automatically):**

* `active_record` - Database queries
* `action_controller` - HTTP requests

**Additional subscribers (opt-in):**

* `active_job` - Background jobs
* `action_mailer` - Email delivery

**Enable additional subscribers:**

```ruby
Sentry.init do |config|
  config.dsn = "___PUBLIC_DSN___"
  config.enable_logs = true

  config.rails.structured_logging.subscribers = {
    active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber,
    action_controller: Sentry::Rails::LogSubscribers::ActionControllerSubscriber,
    active_job: Sentry::Rails::LogSubscribers::ActiveJobSubscriber,
    action_mailer: Sentry::Rails::LogSubscribers::ActionMailerSubscriber
  }
end
```

**Disable default subscribers:**

```ruby
config.rails.structured_logging.subscribers = {
  active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber
  # ActionController disabled
}
```

**Disable structured logging entirely:**

```ruby
config.rails.structured_logging.enabled = false
```

### [Sensitive Data Filtering](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#sensitive-data-filtering)

Structured logging automatically respects Rails' parameter filtering:

`config/application.rb`

```ruby
config.filter_parameters += [:password, :credit_card, :ssn]
```

This filters sensitive data from ActionController parameters, ActiveJob arguments, and ActionMailer parameters.

### [Custom Log Subscribers](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#custom-log-subscribers)

You can create custom subscribers to capture your own Rails instrumentation events:

```ruby
class MyCustomSubscriber < Sentry::Rails::LogSubscriber
  attach_to :my_component

  def my_event(event)
    log_structured_event(
      message: "Custom event occurred",
      level: :info,
      attributes: {
        duration_ms: event.duration,
        user_id: event.payload[:user_id]
      }
    )
  end
end
```

Register it in your configuration:

```ruby
config.rails.structured_logging.subscribers = {
  active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber,
  my_component: MyCustomSubscriber
}
```

### [Using `Rails.logger`](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#using-railslogger)

If you want all Rails logs to go to Sentry, you can enable the `:logger` patch:

```ruby
Sentry.init do |config|
  config.dsn = "___PUBLIC_DSN___"
  config.enable_logs = true
  config.enabled_patches << :logger
end
```

This will send all `Rails.logger` calls to Sentry:

```ruby
Rails.logger.info("Processing user request")
Rails.logger.error("Failed to save record")
```

This captures ALL Rails framework logs as well as ANY OTHER logs produced by instances of the `Logger` class and this can be very noisy. We recommend using `Sentry.logger` for better control over what gets logged.

## [Default Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#default-attributes)

The Rails SDK automatically sets several default attributes on all log entries to provide context and improve debugging:

### [Core Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#core-attributes)

* `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
* `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
* `sdk.name`: The name of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.name`.
* `sdk.version`: The version of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.version`.

### [Message Template Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#message-template-attributes)

If the log was parameterized, Sentry adds the message template and parameters as log attributes.

* `message.template`: The parameterized template string. This is sent from the SDK as `sentry.message.template`.
* `message.parameter.X`: The parameters to fill the template string. X can either be the number that represent the parameter's position in the template string (`sentry.message.parameter.0`, `sentry.message.parameter.1`, etc) or the parameter's name (`sentry.message.parameter.item_id`, `sentry.message.parameter.user_id`, etc). This is sent from the SDK as `sentry.message.parameter.X`.

### [Server Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#server-attributes)

* `server.address`: The address of the server that sent the log. Equivalent to `server_name` that gets attached to Sentry errors.

### [User Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#user-attributes)

If user information is available in the current scope, the following attributes are added to the log:

* `user.id`: The user ID.
* `user.name`: The username.
* `user.email`: The email address.

### [Integration Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#integration-attributes)

If a log is generated by an SDK integration, the SDK will set additional attributes to help you identify the source of the log.

* `origin`: The origin of the log. This is sent from the SDK as `sentry.origin`.

### [Rails-Specific Attributes](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#rails-specific-attributes)

When using structured logging with Log Subscribers, additional Rails-specific attributes are automatically included:

#### [ActiveRecord Logs](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#activerecord-logs)

* `sql` - The SQL query executed
* `duration_ms` - Query execution time in milliseconds
* `cached` - Whether the query result was cached
* `statement_name` - SQL statement name (when available and not "SQL")
* `connection_id` - Database connection identifier (when available)
* `db_system` - Database adapter (e.g., "postgresql", "mysql2", "sqlite3")
* `db_name` - Database name (sanitized for SQLite file paths)
* `server_address` - Database host (when available)
* `server_port` - Database port (when available)
* `server_socket_address` - Database socket path (when available)

#### [ActionController Logs](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#actioncontroller-logs)

* `controller` - Controller class name
* `action` - Action method name
* `duration_ms` - Request processing time in milliseconds
* `method` - HTTP request method
* `path` - Request path
* `format` - Response format (html, json, etc.)
* `status` - HTTP response status code (when available)
* `view_runtime_ms` - View rendering time in milliseconds (when available)
* `db_runtime_ms` - Database query time in milliseconds (when available)
* `params` - Filtered request parameters (only when `send_default_pii` is enabled)

#### [ActiveJob Logs (when enabled)](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#activejob-logs-when-enabled)

**Common attributes for all ActiveJob events:**

* `job_class` - Job class name
* `job_id` - Unique job identifier
* `queue_name` - Queue name where job is processed
* `executions` - Number of execution attempts
* `priority` - Job priority

**For `perform` events:**

* `duration_ms` - Job execution time in milliseconds
* `adapter` - Queue adapter class name
* `scheduled_at` - When job was scheduled in ISO8601 format (for delayed jobs)
* `delay_ms` - Delay between scheduling and execution in milliseconds (for delayed jobs)
* `arguments` - Job arguments (only when `send_default_pii` is enabled, filtered for sensitive data)

**For `enqueue` events:**

* `adapter` - Queue adapter class name (when available)
* `scheduled_at` - When job was scheduled in ISO8601 format (for delayed jobs)
* `delay_seconds` - Delay between current time and scheduled time in seconds (for delayed jobs)

**For `retry_stopped` and `discard` events:**

* `error_class` - Error class name (when error is present)
* `error_message` - Error message (when error is present)

#### [ActionMailer Logs (when enabled)](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#actionmailer-logs-when-enabled)

**For `deliver` events:**

* `mailer` - Mailer class name
* `duration_ms` - Email delivery time in milliseconds
* `perform_deliveries` - Whether deliveries are enabled
* `delivery_method` - Email delivery method used (when available)
* `date` - Email date header as string (when available)
* `message_id` - Email message ID (only when `send_default_pii` is enabled)

**For `process` events:**

* `mailer` - Mailer class name
* `action` - Mailer action method name
* `duration_ms` - Email processing time in milliseconds
* `params` - Mailer parameters (only when `send_default_pii` is enabled, filtered for sensitive data)

## [Troubleshooting](https://docs.sentry.io/platforms/ruby/guides/rails/logs.md#troubleshooting)

If logs aren't appearing in Sentry:

**Verify configuration:**

```ruby
Sentry.init do |config|
  config.enable_logs = true  # Must be set
end
```

**Test your setup:**

```ruby
Sentry.logger.info("Test log message")
Sentry.get_current_client.flush
```

Then verify if the log appears in Sentry.

**Enable debug mode:**

```ruby
config.debug = true  # See SDK diagnostic output
```
