• /
  • Log in
  • Free account

Ruby: Configure logs in context

Logs in context for the Ruby agent connects your logs and APM data in New Relic. Bringing all of this data together in a single tool helps you quickly get to the root cause of an issue and find the log lines that you need to identify and resolve a problem.

Set up your Ruby app

To enable logs in context for APM apps monitored by Ruby:

  1. Make sure you have already set up logging in New Relic. This includes configuring a supported log forwarder that collects your application logs and extends the metadata that is forwarded to New Relic.
  2. Install or update to the latest Ruby agent version, and enable distributed tracing. Use Ruby agent version 6.7.0 or higher for logs in context.
  3. For Rails applications, use a supported Rails version.
  4. Configure logs in context for Ruby.
  1. To verify that you have configured the log appender correctly, run your application, then check your logs data in New Relic One using the query operator has:span.id has:trace.id.

If everything is configured correctly and your data is being forwarded to New Relic with the enriched metadata, your logs should now be emitted as JSON and contain trace.id and span.id fields.

If you have configured your logging in /config/application.rb or in /config/environments/development.rb, run your application locally and check its logging output. You should see some output like this:

{"entity.name":"your_app_name","entity.type":"SERVICE","hostname":"79bcbf8d","trace.id":"79bcbf8d","span.id":"00fc7d46","timestamp":1567701375543,"message":"example log message one","log.level":"DEBUG"}
{"entity.name":"your_app_name","entity.type":"SERVICE","hostname":"79bcbf8d","trace.id":"79bcbf8d","span.id":"6754870b","timestamp":1567702843604,"message":"example log message two","log.level":"DEBUG"}


If you don't see log data in the UI, follow the troubleshooting procedures. Also, if you see JSON logs in your application's output, but your query does not return logs, check your log forwarder.

If the logs from your application are not formatted in JSON with fields like trace.id and span.id, there may be a problem with your log forwarding extension. In this situation:

  • Check that the application is using a supported logging framework.
  • Check that your logging configuration has been applied to all the environments where you want to forward and enrich the log data being sent to New Relic.
  • Check that another logger is not configured later in your application's configuration.

Logs in context for Ruby does not support tagged logging. If you are initializing your logger with a log_tags argument, your custom tags may not appear on the final version of your logs.

What's next?

After you set up APM logs in context, make the most of your logging data:

Create issueEdit page
Copyright © 2022 New Relic Inc.