Skip to content

Instantly share code, notes, and snippets.

@kotay
Last active January 25, 2022 07:32
Show Gist options
  • Save kotay/05615286b1727169c96d to your computer and use it in GitHub Desktop.
Save kotay/05615286b1727169c96d to your computer and use it in GitHub Desktop.
Heroku to Logstalgia formatter
#!/usr/bin/env ruby
# Install:
# $ cat heroku_logstalgia_formatter.rb > ~/bin/heroku_logstalgia_formatter
# $ chmod +x ~/bin/heroku_logstalgia_formatter
# Usage:
# $ heroku logs -t --app {app} | heroku_logstalgia_formatter | logstalgia -
require 'optparse'
require 'pry'
require 'date'
$stdout.sync = true
# Keep reading lines of input as long as they're coming.
while input = ARGF.gets
input.each_line do |line|
parts = line.split
next unless parts.size == 13
timestamp = DateTime.parse(parts[0]).to_time.to_i
ip = parts[7].gsub("fwd=","").gsub('"','')
request = parts[4].gsub("path=","").gsub('"','')
status = parts[11].gsub("status=","")
size = parts[12].gsub("bytes=","")
output_line = "#{timestamp}|#{ip}|#{request}|#{status}|#{size}"
begin
$stdout.puts output_line
rescue Errno::EPIPE
exit(74)
end
end
end
@jgillman
Copy link

jgillman commented Oct 3, 2014

Works like a charm. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment