---
title: "Rails 5 supports logging errors with tagged logging"
description:
  "Rails 5 now generates the application log with log tags applied to errors as
  well"
canonical_url: "https://www.bigbinary.com/blog/rails-5-supports-logging-errors-with-tagged-logging"
markdown_url: "https://www.bigbinary.com/blog/rails-5-supports-logging-errors-with-tagged-logging.md"
---

# Rails 5 supports logging errors with tagged logging

Rails 5 now generates the application log with log tags applied to errors as
well

- Author: Sharang Dashputre
- Published: June 28, 2016
- Categories: Rails 5, Rails

We use
[tagged logging](http://guides.rubyonrails.org/debugging_rails_applications.html#tagged-logging)
to better extract information from logs generated by Rails applications.

Consider a Rails 4.x application where the request id is used as a log tag by
adding following in `config/environments/production.rb`.

```ruby
config.log_tags = [:uuid]
```

The log generated for that application would look like:

```ruby
[df88dbaa-50fd-4178-85d7-d66279ea33b6] Started GET "/posts" for ::1 at 2016-06-03 17:19:32 +0530
[df88dbaa-50fd-4178-85d7-d66279ea33b6] Processing by PostsController#index as HTML
[df88dbaa-50fd-4178-85d7-d66279ea33b6]   Post Load (0.2ms)  SELECT "posts".* FROM "posts"
[df88dbaa-50fd-4178-85d7-d66279ea33b6]   Rendered posts/index.html.erb within layouts/application (4.8ms)
[df88dbaa-50fd-4178-85d7-d66279ea33b6] Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.2ms)
[df88dbaa-50fd-4178-85d7-d66279ea33b6]
ActionView::Template::Error (divided by 0):
    29: <br>
    30:
    31: <%= link_to 'New Post', new_post_path %>
    32: <%= 1/0 %>
  app/views/posts/index.html.erb:32:in `/'
  app/views/posts/index.html.erb:32:in `_app_views_posts_index_html_erb___110320845380431566_70214104632140'
```

As we can see the request id tag is not prepended to the lines containing error
details. If we search the log file by the request id then the error details
would not be shown.

In Rails 5
[errors in logs show log tags as well](https://github.com/rails/rails/pull/23203)
to overcome the problem we saw above.

Please note that the log tag name for request id has changed in Rails 5. The
setting would thus look like as shown below.

```ruby

config.log_tags = [:request_id]

```

This is how the same log will look like in a Rails 5 application.

```ruby

[7efb4d18-8e55-4d51-b31e-119f49f5a410] Started GET "/" for ::1 at 2016-06-03 17:24:59 +0530
[7efb4d18-8e55-4d51-b31e-119f49f5a410] Processing by PostsController#index as HTML
[7efb4d18-8e55-4d51-b31e-119f49f5a410]   Rendering posts/index.html.erb within layouts/application
[7efb4d18-8e55-4d51-b31e-119f49f5a410]   Post Load (0.9ms)  SELECT "posts".* FROM "posts"
[7efb4d18-8e55-4d51-b31e-119f49f5a410]   Rendered posts/index.html.erb within layouts/application (13.2ms)
[7efb4d18-8e55-4d51-b31e-119f49f5a410] Completed 500 Internal Server Error in 30ms (ActiveRecord: 0.9ms)
[7efb4d18-8e55-4d51-b31e-119f49f5a410]
[7efb4d18-8e55-4d51-b31e-119f49f5a410] ActionView::Template::Error (divided by 0):
[7efb4d18-8e55-4d51-b31e-119f49f5a410]     29: <br>
[7efb4d18-8e55-4d51-b31e-119f49f5a410]     30:
[7efb4d18-8e55-4d51-b31e-119f49f5a410]     31: <%= link_to 'New Post', new_post_path %>
[7efb4d18-8e55-4d51-b31e-119f49f5a410]     32: <%= 1/0 %>
[7efb4d18-8e55-4d51-b31e-119f49f5a410]
[7efb4d18-8e55-4d51-b31e-119f49f5a410] app/views/posts/index.html.erb:32:in `/'
[7efb4d18-8e55-4d51-b31e-119f49f5a410] app/views/posts/index.html.erb:32:in `_app_views_posts_index_html_erb___1136362343261984150_70232665530320'

```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-supports-logging-errors-with-tagged-logging)
