Created
June 19, 2013 21:36
-
-
Save bmxpert1/5818342 to your computer and use it in GitHub Desktop.
Rails browser based log file tail-er
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdminController < ApplicationController | |
layout 'admin' | |
def logs | |
log = File.join(Rails.root, "log", "#{Rails.env}.log") | |
@lines = `tail -1024 #{log}`.split(/\n/) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#logs{ | |
font-family: monospace; | |
.request-start{ | |
color: blue; | |
font-weight: bold; | |
} | |
.request-end{ | |
color: green; | |
font-weight: bold; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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