Skip to content

Instantly share code, notes, and snippets.

@bmxpert1
Created June 19, 2013 21:36
Show Gist options
  • Save bmxpert1/5818342 to your computer and use it in GitHub Desktop.
Save bmxpert1/5818342 to your computer and use it in GitHub Desktop.
Rails browser based log file tail-er
class AdminController < ApplicationController
layout 'admin'
def logs
log = File.join(Rails.root, "log", "#{Rails.env}.log")
@lines = `tail -1024 #{log}`.split(/\n/)
end
end
require 'open3'
namespace :log do
task :tail => [:environment] do
log = File.join(Rails.root, "log", "#{ Rails.env }.log")
Open3.popen3("tail -f #{log}") do |stdin, stdout, stderr, wait_thr|
begin
stdout.each do |s|
if s =~ /^Started/
s = "<span class='request-start'>#{s}</span>"
elsif s =~ /^Completed/
s = "<span class='request-end'>#{s}</span>"
end
s = "#{s}<br/>"
WebsocketRails[:log].trigger 'update', s
end
rescue Exception => ex
Rails.logger.info ex.message
Rails.logger.info ex.backtrace
ensure
stdin.close
end
exit_status = wait_thr.value
end
end
end
#logs{
font-family: monospace;
.request-start{
color: blue;
font-weight: bold;
}
.request-end{
color: green;
font-weight: bold;
}
}
<%= javascript_include_tag "admin/logs" %>
<%= stylesheet_link_tag "admin/logs" %>
<div id='logs'>
<% @lines.each do |line| %>
<% if line =~ /^Started/ %>
<span class='request-start'><%= line %></span>
<% elsif line =~ /^Completed/ %>
<span class='request-end'><%= line %></span>
<% else %>
<%= line %>
<% end %>
<br/>
<% end %>
</div>
class Logs
constructor: ->
@scroll()
logChannel = window.dispatcher.subscribe 'log'
logChannel.bind 'update', (data) =>
$('#logs').append data
unless (window.scrollY + window.innerHeight) < $("#logs")[0].scrollHeight
@scroll()
scroll: ->
$(document).scrollTop($("#logs")[0].scrollHeight)
$ ->
new Logs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment