Logging From Ruby

AppSignal for Ruby 3.2.0This feature requires AppSignal for Ruby 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.

bundle update appsignal

Configure Logging

Log data must not contain 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.

Rails Logger

You can configure the Rails framework to send logs to AppSignal by placing the code below in the config/environment.rb file before initialization:

# Use AppSignal's logger
Rails.logger = ActiveSupport::TaggedLogging.new(Appsignal::Logger.new("rails"))
 
# Initialize the Rails application.
Rails.application.initialize!

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:

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

Ruby Logger

To send logs to AppSignal as you would with Ruby's logging 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:

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 unknown, fatal, error, warn, info, and debug:

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:

logger = Appsignal::Logger.new("invoice_helper")
logger.info("Generating invoice for customer #{@customer.id}", { customer_id: @customer.id, date: DateTime.now })
invoice = generate_invoice(@customer)
logger.info("Generated invoice for customer #{@customer.id}", { 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.

Logfmt and JSON

Logfmt and JSON support is available in AppSignal for Ruby version 3.3.6 and later.

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

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

For JSON:

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

A popular way of ingesting structured Rails logs is using Lograge.

After adding the lograge gem you can use config like this in config/initializers/lograge.rb to ingest the logs:

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

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!

Need more help?

Contact us and speak directly with the engineers working on AppSignal. They will help you get set up, tweak your code and make sure you get the most out of using AppSignal.

Contact us

Start a trial - 30 days free

AppSignal is a great way to monitor your Ruby, Elixir & Node.js applications. Works great with Rails, Phoenix, Express and other frameworks, with support for background jobs too. Let's improve your apps together.

Start a trial