Created
August 5, 2013 16:48
-
-
Save ldmosquera/6157443 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
#usage: tail_logs.rb APP_NAME | |
#merely tails the log to STDOUT, exiting on errors. Intended to be run from some kind of loop. | |
#Auth: requires ENV['HEROKU_API_KEY'] to be set. | |
require 'bundler' | |
Bundler.require | |
app_name = ARGV[0] | |
class LogTailer | |
def initialize(app_name) | |
@app_name = app_name | |
begin | |
tail | |
rescue Exception=>e | |
STDERR.puts e | |
end | |
end | |
private | |
def tail | |
buffer = "" | |
request(get_log_url).stream do |chunk| | |
buffer << chunk | |
while line = buffer.slice!(/.+\n/) | |
puts line | |
end | |
end | |
end | |
def request(url) | |
EventMachine::HttpRequest.new(url, keepalive: true, connection_timeout: 15, inactivity_timeout: 0).get | |
end | |
def get_log_url | |
api.get_logs(@app_name, {'tail' => 1, 'num' => 1500}).body | |
end | |
def api | |
@api ||= Heroku::API.new | |
end | |
end | |
EventMachine.run { | |
LogTailer.new app_name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment