Set Up Logs

Structured logs allow you to send, view and query logs sent from your applications within Sentry.

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

Logs for Ruby are supported in Sentry Ruby SDK version 5.24.0 and above.

Copied
gem install sentry-ruby

Or add it to your Gemfile:

Copied
gem "sentry-ruby"

To enable logging, you need to initialize the SDK with the enable_logs option set to true.

Copied
Sentry.init do |config|
  config.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
  config.enable_logs = true
end

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the Sentry.logger APIs.

The logger namespace exposes six methods that you can use to log messages at different log levels: trace, debug, info, warning, error, and fatal.

You can pass additional attributes directly to the logging functions. These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.

Copied
Sentry.logger.info("Updated global cache")

Sentry.logger.debug("Cache miss for user %{user_id}", user_id: 123)

Sentry.logger.trace(
  "Starting database connection %{database}",
  database: "users"
)

Sentry.logger.warn(
  "Rate limit reached for endpoint %{endpoint}",
  endpoint: "/api/results/"
)

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

Sentry.logger.fatal(
  "Database %{database} connection pool exhausted",
  database: "users"
)

You can also use message templates with positional or hash parameters:

Copied
# Using named parameters
Sentry.logger.info("User %{name} logged in", name: "Jane Doe")

# Using positional parameters
Sentry.logger.info("User %s logged in", ["Jane Doe"])

Any other arbitrary attributes will be sent as part of the log event payload:

Copied
# Here `user_id` and `action` will be sent as extra attributes that
# Sentry Logs UI displays
Sentry.logger.info(
  "User %{user} logged in",
  user: "Jane", user_id: 123, action: "create"
)

To enable logging for all loggers that use Ruby Logger you can enable :logger patch:

Copied
Sentry.init do |config|
  config.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
  config.enable_logs = true
  config.enabled_patches = [:logger]
end

Then all logs from Logger instances will be sent to Sentry, for example:

Copied
logger = Logger.new($stdout)

logger.debug("Hello from stdlib logger")
logger.info("Hello from stdlib logger")
logger.error("Hello from stdlib logger")

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:

Copied
# 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")
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").