Logging From Ruby

This feature requires AppSignal for Ruby version 3.2.0 or higher.

This documentation outlines how to configure logging with your AppSignal for Ruby integration.

Installation

To use logging with your AppSignal integration for Ruby, you first need to upgrade to AppSignal for Ruby gem version 3.2.0 or newer. To do this, run the command below. Afterwards, make sure it's updated to the latest version in the Bundler output.

Shell
bundle update appsignal

Configure Logging

Your log data must not contain any personal data, such as names, email addresses, etc. It is your responsibility to ensure this data is filtered out before being sent to AppSignal, and when identifying a person is necessary that your application uses alternative forms of identification such as a user ID, hash, or pseudonym.

You do not need to create a log source to send logs from the AppSignal for Ruby integration. An "application" log source will be created automatically.

Rails Logger

This feature requires AppSignal for Ruby version 3.4.0 or higher.

You can configure the Rails framework to send logs to AppSignal in an initializer file like config/initializers/logging.rb:

For Rails 7.1 and newer, use the broadcast_to method to configure the AppSignal logger.

Ruby
# config/initializers/logging.rb appsignal_logger = Appsignal::Logger.new("rails") Rails.logger.broadcast_to(appsignal_logger)

For Rails 7.0 and older, use the example below to add the AppSignal logger.

Shell
# Use AppSignal's logger Rails.logger = ActiveSupport::TaggedLogging.new(Appsignal::Logger.new("rails"))

The use of ActiveSupport::TaggedLogging adds support for Rails' log tags. For example, if you wish to add the request ID to the log messages, add it to the config.log_tags array in the config/application.rb file:

Ruby
module TestApp class Application < Rails::Application config.log_tags = [:request_id] end end

Using multiple logging backends

To send Rails logs to multiple destinations, such as AppSignal, terminal output, a secondary log file, or any other service, use the Rails logger broadcast feature.

In the example below, Rails is configured to send logs to both AppSignal and the terminal output (STDOUT):

For Rails 7.1 and newer, use the broadcast_to method to configure an extra logger.

Ruby
# config/initializers/logging.rb appsignal_logger = Appsignal::Logger.new("rails") Rails.logger.broadcast_to(appsignal_logger)

For Rails 7.0 and older, use the ActiveSupport::Logger.broadcast helper.

Ruby
# config/initializers/logging.rb # Use AppSignal's logger and a STDOUT logger console_logger = ActiveSupport::Logger.new(STDOUT) appsignal_logger = ActiveSupport::TaggedLogging.new(Appsignal::Logger.new("rails")) Rails.logger = console_logger.extend(ActiveSupport::Logger.broadcast(appsignal_logger))

Sidekiq Logger

You can also configure Sidekiq to send its logs to AppSignal, including the logs emitted by your background jobs:

Ruby
Sidekiq.configure_server do |config| config.logger = Appsignal::Logger.new("sidekiq") config.logger.formatter = Sidekiq::Logger::Formatters::WithoutTimestamp.new end

Ruby Logger

To send logs to AppSignal as you would with Ruby's Logger class, you will need to create an instance of Appsignal::Logger and provide it with a groupname that defines where you are logging from, for example, if we were logging from a helper for invoicing clients:

Ruby
logger = Appsignal::Logger.new("invoice_helper")

Usage

Sending Logs

Like the Ruby/Rails Logger class, you can define the severity level of your logs by using fatal, error, warn, info, and debug:

Ruby
logger.warn("Something's gone terribly wrong here")

You can define custom attributes to send log information that can be used when filtering and querying logs:

Ruby
logger = Appsignal::Logger.new("invoice_helper") logger.info("Generating invoice for customer", { customer_id: @customer.id }) invoice = generate_invoice(@customer) logger.info("Generated invoice for customer", { customer_id: @customer.id, invoice_id: invoice.id })

You can query and filter on message contents and attribute values from within the Log Management tool.

Once configured, the desired attributes will be sent to AppSignal as log_tags, and be queryable in the AppSignal logging interface.

Default Logger Attributes

This feature requires AppSignal for Ruby version 4.2.0 or higher.

When initializing the AppSignal logger, you can provide default attributes that will be included in all log messages:

Ruby
logger = Appsignal::Logger.new("invoice_helper", attributes: { customer_id: @customer.id })

The attributes provided when initializing the logger will be included in all log messages sent from that logger instance. Attributes provided with the log message will be merged with the default logger attributes.

Ruby
logger = Appsignal::Logger.new("invoice_helper", attributes: { customer_id: @customer.id }) # This log message will contain the `customer_id` attribute: logger.info("Generating invoice for customer") invoice = generate_invoice(@customer) # This log message will contain the `customer_id` and `invoice_id` attributes: logger.info("Generated invoice for customer", { invoice_id: invoice.id })

When both the logger and the log message provide the same attribute, the log message's attribute will override the default logger attribute.

Logfmt and JSON

This feature requires AppSignal for Ruby version 3.3.6 or higher.

The log format can be specified when creating a logger. For Logfmt:

Ruby
logger = Appsignal::Logger.new("group", format: Appsignal::Logger::LOGFMT) logger.info("category=blog this is about the blog")

For JSON:

Ruby
logger = Appsignal::Logger.new("group", format: Appsignal::Logger::JSON) logger.info('{"category": "blog", "message": "this is about the blog"}')

In both of the examples above, a filterable category attribute with the value "blog" will be logged.

Lograge

This Lograge config is not a replacement for the Rails logger config. If you want to report logs from Rails.logger, the Rails logger config steps also need to be followed.

The Lograge library is a popular solution for ingesting structured Rails request logs.

After adding the lograge gem, you can configure it in config/initializers/lograge.rb to send logs to AppSignal:

Ruby
Rails.application.configure do config.lograge.enabled = true config.lograge.keep_original_rails_log = true config.lograge.logger = Appsignal::Logger.new( "rails", format: Appsignal::Logger::LOGFMT ) end

Lograge does not support log levels. All logs emitted via Lograge will appear with severity level INFO in the AppSignal Logging UI.

Semantic Logger

The Semantic Logger library provides a comprehensive logging solution for Ruby and Rails applications.

After adding the semantic_logger gem, you can configure it to send logs to AppSignal as follows:

Ruby
appsignal_logger = Appsignal::Logger.new("rails", format: Appsignal::Logger::LOGFMT) SemanticLogger.add_appender(logger: appsignal_logger, formatter: :logfmt)

Filtering Logs

You can use the ignore_logs configuration option to ignore log lines. See our Ignore Logs guide to learn more.

Need Help?

After configuring your Ruby application to send logs, logs should appear in AppSignal. If you are unsure about this step or AppSignal is not receiving any logs, you can always reach out for assistance. We'll help get you back on track!