# Sending Logs From Your Heroku App to Datadog

Source: https://www.ironin.it/blog/sending-logs-from-your-heroku-app-to-datadog.html

Published 13 July 2018

[web](https://www.ironin.it/blog/tags/web) [back-end development](https://www.ironin.it/blog/tags/back-end-development) [ruby](https://www.ironin.it/blog/tags/ruby) [rails](https://www.ironin.it/blog/tags/rails) [heroku](https://www.ironin.it/blog/tags/heroku)

![Sending Logs From Your Heroku App to Datadog](https://www.ironin.it/api/media/file/ironin-seed-ZjVlMzJkNzgtZjY5Yi00MDdhLTk0NTctZjdmNTVmYmEzZTMw_sending-logs-from-your-heroku-app-to-datadog.jpg)

Are you using Datadog and Heroku? Would you like to send your Ruby on Rails application’s logs to Datadog for history purposes and easier processing? We will show you how to do it.

If you are not familiar with [Datadog](https://www.datadoghq.com/), it’s a very functional monitoring and analytics solution that provides a better overview of your applications.

Since it’s not possible to directly connect [Heroku drains](https://devcenter.heroku.com/articles/log-drains) with Datadog logs (at least at the time of writing this article) we have to use a different solution. Up until recently it also wasn’t possible to customize the [Datadog buildpack for Heroku](https://github.com/DataDog/heroku-buildpack-datadog), besides setting environment variables (which is useful but quite limited). Thanks to one of our Ronins [pull request](https://github.com/DataDog/heroku-buildpack-datadog/pull/49) on the GitHub project, you now are able to provide an advanced configuration as part of your build process.

## Datadog configuration

In the latest version of the buildpack you can customize the agent by writing configuration files in the \`datadog/conf.d\` directory in the root of your application.

Configuration files are simple \`yaml\` files and the buildpack will search for \`\*.yaml\` files in the \`conf.d\` directory, then copy them to \`$HOME/.apt/etc/datadog-agent/conf.d/${filename}.d/conf.yaml\` so they can be picked up correctly by the agent upon start.

List of all available options for logs collection can be found [here](https://docs.datadoghq.com/logs/log_collection/).

## Pushing your application logs

To push your logs you need to:

1\\. Add the Datadog buildpack to your Heroku application:

```bash
heroku buildpacks:add --index 1 https://github.com/DataDog/heroku-buildpack-datadog.git
```

2\\. Provide the \`DD_API_KEY\` ENV variable with your application’s key:

```bash
heroku config:add DD_API_KEY=<your API key>
```

3\\. Enable the logging feature - it can be done by setting the \`DD_LOGS_ENABLED\` variable:

```bash
heroku config:add DD_LOGS_ENABLED=true
```

4\\. Provide a configuration for the agent to watch for your log files and push them to Datadog (configuration goes to to the \`/app/datadog/conf.d/logs.yaml\` file):

```yml
##Log section
logs:
  - type: file
    path: /app/log/*.log
    source: ruby
    sourcecategory: sourcecode
```

5\\. Ensure that either \`RAILS_LOG_TO_STDOUT\` variable is not set - or you are using \`ActiveSupport::Logger.broadcast\` to push your logs to STDOUT and file at the same time (the Datadog agent needs to watch log files in order to push them).

6\\. Specify tags for Datadog (optional but can help with managing your logs) by providing the \`DD_TAGS\` variable (i.e. \`env:production\`).

We also recommend to set the \`DD_LOG_LEVEL\` to \`critical\` to prevent swamping your output (i.e. when you connect to your container with the essential \`heroku run bash\` command) with messages from Datadog agent:

```bash
heroku config:add DD_LOG_LEVEL=critical
```

## Bonus: logging to STDOUT and log file

If you would like to be able to check your Heroku logs the standard way (\`heroku logs\` \`--\`\`tail\`), as well as keep them in the Datadog, you need to push your messages to STDOUT and log file at the same time. This can be easily achieved with the \`ActiveSupport::Logger.broadcast\` method, which overrides original logging methods so that he messages can be pushed to the additional logger.

The code is as simple as this:

```ruby
if ENV["RAILS_LOG_TO_STDOUT"].present?
  logger           = ActiveSupport::Logger.new(STDOUT)
  logger.formatter = Rails.application.config.log_formatter
  logger           = ActiveSupport::TaggedLogging.new(logger)
  Rails.logger.extend(ActiveSupport::Logger.broadcast(logger))
end
```

Saving time by building automation tricks instead of performing manual transfers always gives us a buzz. Does your DevOps configuration need tweaking? [Let us](https://www.ironin.it/contact-us.html) analyze your systems and see where you can save time by implementing configuration tweaks. Trust the experts at iRonin to optimize your solutions and drop us a line!

Share:

Author: iRonin IT Team

Need software like this built?

[Software development →](https://www.ironin.it/ai-software-engineering)

Our code is merged into 10 open-source AI-agent projects, two of them with tens of thousands of GitHub stars.

[Talk to us →](https://www.ironin.it/contact-us.html?offer=ai-software-engineering)

## Read similar articles

- [OS Time Travel When Testing Ruby on Rails Web Applications For System-Wide Time Events](https://www.ironin.it/blog/os-time-travel-when-testing-ruby-on-rails-web-applications-for-system-wide-time-events.html)
- [How to Automatically Schedule Your Dokku Database Backups](https://www.ironin.it/blog/how-to-automatically-schedule-your-dokku-database-backups.html)
- [From Data to Decisions: The Practical Applications of Predictive Analytics in Real Estate](https://www.ironin.it/blog/predictive-analytics-real-estate.html)
- [Tutorial - A serverless web service with Node.js](https://www.ironin.it/blog/tutorial-a-serverless-web-service-with-node.html)

## Read next

- [Which AI model is best for your own task? Our own test and a public leaderboard disagreed](https://www.ironin.it/blog/own-ai-model-evals.html)
- [What uses up a Claude plan's usage limit? What each kind of token costs against it](https://www.ironin.it/blog/claude-usage-limit-token-costs.html)
- [Business Self-Saboteur - What Causes Technical Debt and How to Avoid It](https://www.ironin.it/blog/business-self-saboteur-what-causes-technical-debt-and-how-to-avoid-it.html)
