---
title: Ruby agent logs in context
source: https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-ruby
---

With our Ruby APM agent, you can get **logs in context**, which lets you see your app logs in the context of your other New Relic data. For general information on this feature, see [APM logs in context](https://docs.newrelic.com/docs/apm/new-relic-apm/getting-started/get-started-logs-context).

> #### 💡 TIP
>
> Got lots of Ruby logs? Check out our [tutorial on how to optimize and manage them](https://docs.newrelic.com/docs/tutorial-large-logs/get-started-managing-large-logs/).

## Automatic logs in context options [#automatic]

You have three options to configure APM logs in context to send your app's logs and linking metadata automatically to New Relic.

**Option 1 (simplest): Let the agent decorate and forward your logs.**

This is the simplest approach, and it's a great choice for developers who may not have the access or interest in setting up a log forwarder, or for accounts that want to see the power of logs and other linking metadata in context of their apps, without a lot of overhead.

Using this option, the agent adds `span.id`, `trace.id`, `hostname`, `entity.guid`, and `entity.name`. [Learn about log forwarding limitations.](https://docs.newrelic.com/docs/logs/logs-context/get-started-logs-context#forwarding-details)

All you need to do is install an agent version with log forwarding capabilities ([Ruby agent 8.6.0 or higher](https://docs.newrelic.com/docs/release-notes/agent-release-notes/ruby-release-notes/)). If forwarding is disabled, you can use this configuration:

newrelic.yml:

````yml
application_logging:
  forwarding:
    enabled: true
```

Environment variable:

```ini
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED=true
```

This option is enabled by default in version 8.7.0 and later. To disable log forwarding, you must set the value to `false`.

<DNT>
  **Optional adjustments:**
</DNT>

Once this is enabled, you also have control over the maximum number of logs sent to New Relic every minute. The default value is 10,000. If more than 10,000 logs are received in a 60-second window, your logs will begin to be sampled.

Set it to a higher number to receive more logs. Set it to a lower number to receive fewer logs. Any integer up to 100,000 is valid.

newrelic.yml:

```yml
application_logging:
  forwarding:
    max_samples_stored: 10000
```

Environment variable:

```ini
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_MAX_SAMPLES_STORED=10000
```

If you have an existing log forwarding solution and are updating your agent to use automatic logs in context, be sure to <DNT>**disable your manual log forwarder**</DNT>. Otherwise, your app will be sending double log lines. Depending on your account, this could result in double billing. For more information, follow the procedures to disable your [specific log forwarder](/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding).

````

**Option 2: Let the agent decorate your logs.**

Already have a log forwarder you like? We've got you covered! Language agents can decorate your logs with the linking metadata needed to provide access to automatic logs-in-context features.

This option should not be used with in-agent forwarding. Using an [external log forwarder](https://docs.newrelic.com/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding) to send logs to New Relic while in-agent forwarding is enabled will cause your logs to be sent up twice to New Relic. Depending on your account, this may result in double billing.

This option should also not be used with [the manual log decorating formatter](#3-old-formatter). If you have references to the manual formatter in your codebase, please remove them before enabling this option.

1.  If you want to use this option, make sure you have the in-agent forwarding configuration option disabled.

    newrelic.yml:

    ```yaml
     application_logging:
       forwarding:
         enabled: false
    ```

2.  Enable log decorating in your configuration, then relaunch the agent to start decorating your logs.

    newrelic.yml:

    ```yaml
    application_logging:
      local_decorating:
        enabled: true
    ```

    Environment variable:

    ```ini
    NEW_RELIC_APPLICATION_LOGGING_LOCAL_DECORATING_ENABLED=true
    ```

    Our decorator adds five attributes to every log message: `entity.guid`, `entity.name`, `hostname`, `trace.id`, and  `span.id`. Example:

    ```
    This is my log message. NR-LINKING|{entity.guid}|{hostname}|{trace.id}|{span.id}|{entity.name}|
    ```

    If the log message is empty or blank, output message will also be empty. Example:

    ```
    NR-LINKING|{entity.guid}|{hostname}|{trace.id}|{span.id}|{entity.name}|.
    ```

    The output log message will be an empty string.

    Some attributes may be empty if the log occurred outside a transaction or if they are not applicable to your application's context.

    We recommend this option over the manual decorating formatter, `NewRelic::Agent::Logging::DecoratingFormatter`.

**Option 3: Use the manual process to forward and decorate logs.**

Before language agents had the ability to forward and decorate logs, you could enable logs in context by updating your application to use the `NewRelic::Agent::Logging::DecoratingFormatter` to send linking metadata.

This option is still supported, but is no longer encouraged. For instructions to use this approach, see [Manual logs in context option](#configure-ruby).

Also, this method requires that you install a log forwarder before enabling logs in context. If you do not have a log forwarder, the New Relic UI will prompt you to use our [infrastructure agent](https://docs.newrelic.com/docs/logs/forward-logs/forward-your-logs-using-infrastructure-agent/).

If you decide to use your existing log forwarding solution and later decide to update your agent to use automatic logs in context, be sure to **disable your manual log forwarder**. Otherwise, your app will be sending double log lines. Depending on your account, this could result in double billing. For more information, follow the procedures to disable your [specific log forwarder](https://docs.newrelic.com/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding).

## Secure your data [#obfuscation]

Your logs may include sensitive information protected by HIPAA or other compliance protocols. By default we obfuscate number patterns that appear to be for items such as credit cards or Social Security numbers, but you may need to hash or mask additional information.

For more information, see our documentation about [obfuscation expressions and rules](https://docs.newrelic.com/docs/logs/ui-data/obfuscation-ui/). You can hash or mask your log data by using the New Relic UI or by using NerdGraph, our GraphQL API.

## Explore your data [#view-ui]

To make the most of your logging data:

-   On the [APM **Summary** page](https://docs.newrelic.com/docs/apm/apm-ui-pages/monitoring/apm-summary-page-view-transaction-apdex-usage-data), click your **Web transaction time** chart to view logs associated with a specific point in time.
-   Check your app's [**Errors inbox**](https://docs.newrelic.com/docs/errors-inbox/errors-inbox) to view the logs associated with your errors.
-   Use [distributed tracing](https://docs.newrelic.com/docs/distributed-tracing/ui-data/understand-use-distributed-tracing-ui) to see the logs associated with individual traces.
-   Explore more logging data across your platform with our [logs UI](https://docs.newrelic.com/docs/logs/log-management/ui-data/use-logs-ui/).
-   Set up [alerts](https://docs.newrelic.com/docs/alerts-applied-intelligence/new-relic-alerts/alert-conditions/create-alert-conditions/) based on log output and severity.
-   [Query your data](https://docs.newrelic.com/docs/logs/ui-data/query-syntax-logs/) using our specialized UI for logs data, and [create dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards/) with the results.

## Troubleshooting tips [#troubleshooting]

Typically your logs will start to appear less than a minute after you enable APM logs in context. Check your app's **Triage > Logs** section. You will also start seeing [log patterns](https://docs.newrelic.com/docs/logs/ui-data/find-unusual-logs-log-patterns) related to the error there.

If you don't see any logs for errors or traces, there may not be any for your app. Try refreshing the UI page, or change the [selected time period](https://docs.newrelic.com/docs/new-relic-one/ui-data/basic-ui-features/#data-analysis).

## Disable automatic logging [#disable-automatic]

APM logs in context automatically forwards APM agent log data and is enabled by default. This can have a negative impact on your security, compliance, billing, or system performance. For more information, or if you need to adjust the default setting, follow the procedures to [disable automatic logging](https://docs.newrelic.com/docs/logs/logs-context/disable-automatic-logging).

## Manual logs in context option [#configure-ruby]

If you need to use the manual process to set up logs in context for Ruby, follow these steps:

1.  Make sure you have already [set up logging in New Relic](https://docs.newrelic.com/docs/logs/enable-log-management-new-relic/enable-log-monitoring-new-relic/enable-log-management-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](https://docs.newrelic.com/docs/agents/ruby-agent/installation/install-new-relic-ruby-agent/) or [update](https://docs.newrelic.com/docs/agents/ruby-agent/installation/update-ruby-agent/) to the latest Ruby agent version, and [enable distributed tracing](https://docs.newrelic.com/docs/distributed-tracing/enable-configure/quick-start/). Use [Ruby agent version 6.7.0 or higher](https://docs.newrelic.com/docs/release-notes/agent-release-notes/ruby-release-notes/) for logs in context.

3.  For Rails applications, use a [supported Rails version](https://docs.newrelic.com/docs/agents/ruby-agent/getting-started/ruby-agent-requirements-supported-frameworks/#web_frameworks).

4.  Configure logs in context for Ruby.

    **Standard Ruby on Rails configuration**

    Rails logging is controlled by two components:

    -   A logger you can customize by setting `config.logger`
    -   A log formatter you can customize by setting `config.log_formatter`)

        In most cases, you should configure logs in context by setting `config.log_formatter` to the `DecoratingFormatter` in your Rails application. For more information about Rails configuration, see the [rubyonrails.org documentation](https://guides.rubyonrails.org/configuring.html).

        In your application's config, require `newrelic_rpm`, then add the following line:

        ```ruby
        module ________
          class Application < Rails::Application

          ...

          config.log_formatter = ::NewRelic::Agent::Logging::DecoratingFormatter.new
          end
        end
        ```

        This configuration uses the New Relic formatter for log messages, but the remaining configuration is provided by the other Rails settings.

    **Incompatible gems**

    New Relic's decorating logger is known to be incompatible with the following gems:

    -   [logging](https://rubygems.org/gems/logging)
    -   [semantic logger](https://rubygems.org/gems/semantic_logger)
    -   [rails_stdout_logger](https://rubygems.org/gems/rails_stdout_logging)
    -   [rails_12factor](https://rubygems.org/gems/rails_12factor)

    **Rails advanced configuration**

    If setting the `log_formatter` option doesn't meet your needs, replace the entire Rails logger with an instance of the New Relic logger. Provide the parameters to the logger's constructor, like this:

    ````ruby
    module ________
      class Application < Rails::Application

        ...

        config.logger = ::NewRelic::Agent::Logging::DecoratingLogger.new(
        "log/application.log", #etc...
        )
      end
    end
    ```

    ````

    **Ruby configuration without Rails**

    For non-Rails applications, use the `DecoratingLogger` in place of the Ruby standard `::Logger`, like this:

    ````ruby
    logger = ::NewRelic::Agent::Logging::DecoratingLogger.new(
      'log/application.log', #etc...
    )
    ...

    logger.info(...)
    ```

    The `DecoratingLogger` is a drop-in replacement for the Ruby standard `::Logger`. Their constructors accept the same parameters.

    ````

    **Lograge configuration**

    To configure this extension with the [lograge gem](https://rubygems.org/gems/lograge), follow standard procedures in this doc for [Ruby on Rails configuration](#enable-rails). No additional configuration is required for the lograge gem.

    **Other logging extensions**

    To use our logging extension with a different logging implementation or with your own custom logger, use the `DecoratingFormatter`. For example:

    ````ruby
    module ________
      class Application < Rails::Application

        ...

        config.logger = ::YourCustomLoggerImplementation.new(
          $stdout,
          formatter: ::NewRelic::Agent::Logging::DecoratingFormatter.new
        )
      end
    end
    ```

    ````

5.  To verify that you have configured the log appender correctly, run your application, then check your log data in the New Relic UI 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:

```json
{"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"}
```
