Is there any way to display Rails' log files in the browser?

For reference, there is a very simple way to do this. However, you would want to protect this page with some sort of authentication/authorization.

Controller:

lines = params[:lines]
if Rails.env == "production"
  @logs = `tail -n #{lines} log/production.log`
else
  @logs = `tail -n #{lines} log/development.log`
end

Log File View:

<pre><%= @logs %></pre>

View to display link:

<%= link_to "log file", log_path(lines: 100) %>

Routes:

get 'logs/:lines' => "pages#log", as: "log"

All you need is to open log file and put its content into browser.

Realted topic: ruby: all ways to read from file.

Also you should know that your logs grow very fast, and it's not a good idea to show whole log in browser. You can just open last 100-200 rows. Related topic: Reading the last n lines of a file in Ruby?

Also you can try this solution: http://newrelic.com/. It is more complex and little oftopic, but quite useful.


I created a for this purpose called browserlog.

Installing is just a matter of adding the gem to the Gemfile:

gem 'browserlog'

Afterwards all you need is to mount the engine on the route you want:

MyApp::Application.routes.draw do
  mount Browserlog::Engine => '/logs'
end

Once that's set up, accessing /logs/development (or production, test, etc) will show a window like this:

browserlog_screenshot
(source: andredieb.com)