Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/platforms/ruby/guides/rails/logs/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Set Up Logs
sidebar_title: Logs
description: "Structured logs allow you to send, view and query logs sent from your Rails applications within Sentry."
sidebar_order: 5600
---

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

<PlatformContent includePath="logs/requirements" />

## Setup

<PlatformContent includePath="logs/setup" />

## Usage

<PlatformContent includePath="logs/usage" />

## Options

<PlatformContent includePath="logs/options" />

## Default Attributes

<PlatformContent includePath="logs/default-attributes" />
5 changes: 5 additions & 0 deletions docs/product/explore/logs/getting-started/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
label="Ruby"
url="/platforms/ruby/logs/"
/>
- <LinkWithPlatformIcon
platform="ruby.rails"
label="Rails"
url="/platforms/ruby/guides/rails/logs/"
/>

### Go

Expand Down
80 changes: 80 additions & 0 deletions platform-includes/logs/default-attributes/ruby.rails.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
The Rails SDK automatically sets several default attributes on all log entries to provide context and improve debugging:

<Include name="logs/default-attributes/core" />

<Include name="logs/default-attributes/message-template" />

<Include name="logs/default-attributes/server" />

<Include name="logs/default-attributes/user" />

<Include name="logs/default-attributes/integration" />

### Rails-Specific Attributes

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

#### 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
- `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)

**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)

**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)
10 changes: 0 additions & 10 deletions platform-includes/logs/integrations/ruby.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,4 @@ logger.error("Hello from stdlib logger")
Ruby's stdlib logger does not support structured logging, that's why logs are sent to Sentry as plain messages with default attributes added automatically by the SDK.
</Alert>

## Ruby on Rails

If you enable `:logger` patch like explained above, this will affect Rails' built-in logger. This means that anything that Rails logs, and any custom usage of the Rails logger, will result in sending log entries to Sentry, for example:

```ruby
# All these calls will result in log entries in Sentry
# if :logger patch is enabled
Rails.logger.debug("Hello from Rails logger")
Rails.logger.info("Hello from Rails logger")
Rails.logger.error("Hello from Rails logger")
```
127 changes: 127 additions & 0 deletions platform-includes/logs/options/ruby.rails.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
### Available Built-in Subscribers

The Rails SDK includes several built-in log subscribers that you can enable:

#### Default Subscribers (enabled automatically)
- **ActiveRecord**: Database queries with SQL, duration, connection info, and caching status
- **ActionController**: HTTP requests with controller, action, parameters, response status, and timing

#### Additional Subscribers (opt-in)
- **ActiveJob**: Background job execution (`perform`), enqueueing (`enqueue`), retry failures (`retry_stopped`), and job discarding (`discard`)
- **ActionMailer**: Email delivery (`deliver`) and processing (`process`) events

### Custom Subscriber Configuration

You can customize which subscribers are active:

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

config.enable_logs = true

config.rails.structured_logging.enabled = true

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

To enable additional subscribers, add them to the configuration:

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

config.enable_logs = true

config.rails.structured_logging.enabled = 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
```

To disable specific subscribers:

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

config.enable_logs = true

config.rails.structured_logging.enabled = true

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

### Creating Custom Log Subscribers

You can create custom log subscribers by extending the base class:

```ruby
class MyCustomSubscriber < Sentry::Rails::LogSubscriber
# Attach to your component's instrumentation events
attach_to :my_component

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

def another_event(event)
log_structured_event(
message: "Another custom event",
level: :warn,
attributes: {
event_type: event.payload[:type],
metadata: event.payload[:metadata]
}
)
end
end
```

Then register your custom subscriber:

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

config.enable_logs = true

config.rails.structured_logging.enabled = true

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

### Sensitive Data Filtering

Log subscribers automatically respect Rails' parameter filtering configuration. Sensitive parameters defined in `config.filter_parameters` will be filtered from structured logs:

```ruby
# config/application.rb
config.filter_parameters += [:password, :credit_card, :ssn]
```
11 changes: 11 additions & 0 deletions platform-includes/logs/requirements/ruby.rails.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"
```
66 changes: 66 additions & 0 deletions platform-includes/logs/setup/ruby.rails.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
To enable logging, you need to initialize the SDK with the `enable_logs` option set to `true`.

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

### Structured Logging with Log Subscribers

<Alert level="info" title="Note on structured logging">
Structured logging with Log Subscribers provides rich context and metadata compared to plain logger messages. This enables better filtering, searching, and analysis of your application's behavior in Sentry.
</Alert>

Rails applications can benefit from structured logging using ActiveSupport's Log Subscribers. This feature captures Rails instrumentation events and sends them as structured logs to Sentry with relevant context and metadata.

#### Default Setup

To enable structured logging with default subscribers:

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

By default, this enables structured logging for:

- **ActiveRecord**: Database queries with SQL, duration, connection info, and caching status
- **ActionController**: HTTP requests with controller, action, parameters, response status, and timing

Additional subscribers are available but not enabled by default:

- **ActiveJob**: Background job execution (`perform`), enqueueing (`enqueue`), retry failures (`retry_stopped`), and job discarding (`discard`)
- **ActionMailer**: Email delivery (`deliver`) and processing (`process`) events

See the [Options](#options) section for information on how to enable additional subscribers.

### Basic Rails Logger Integration

If you enable `:logger` patch, this will affect Rails' built-in logger. This means that anything that Rails logs, and any custom usage of the Rails logger, will result in sending log entries to Sentry:

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

Then all logs from Rails logger will be sent to Sentry, for example:

```ruby
# All these calls will result in log entries in Sentry
# if :logger patch is enabled
Rails.logger.debug("Hello from Rails logger")
Rails.logger.info("Hello from Rails logger")
Rails.logger.error("Hello from Rails logger")
```

<Alert level="warning" title="Note on logger patch">
Enabling `:logger` patch will send all logs from Rails logger to Sentry. This includes logs from Rails framework itself, which might not be desired. In that case we recommend using [Sentry logger](#using-sentry-logger) or [structured logging with log subscribers](#structured-logging-with-log-subscribers).
</Alert>
Loading
Loading