This blog is part of our Rails 5 series.

We use 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.

config.log_tags = [:uuid]

The log generated for that application would look like:

[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 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.

config.log_tags = [:request_id]

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

[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'