Is there a way to change log format of Rails app in production?

February 12, 2015 660 views

I have Rails 3 app that runs on Nginx server using Puma. The logs goes to logs/production.log.

I was wondering if there is a way to change the format of the logs, so it includes more information that I can use. Informations like userId (if signed in) or sessionId, etc.

Is there a way to configure the format of the logs?

Right now they are like this:

Started GET "/" for at 2015-02-11 06:37:53 +0000
  Processing by MyController#home as HTML
Rendered my/home.html.erb within layouts/home (93.2ms)
Completed 200 OK in 542ms (Views: 94.8ms | ActiveRecord: 158.6ms)
1 Answer

These two blog posts were really helpful for me in understanding how to configure and modify logging in Rails applications:

At the most basic, you can configure Rails to log more information by setting config.log_level = debug in your config/application.rb But you can also totally customize the output and information available by writing your own formatter. If you're looking to just append a bit more info, you might look at the config.log_tags setting. From the docs:

config.log_tags accepts a list of methods that the request object responds to. This makes it easy to tag log lines with debug information like subdomain and request id - both very helpful in debugging multi-user production applications.

For example, to add a user's UUID to the logs:

config.log_tags = [ :uuid ]
Have another answer? Share your knowledge.