Last active
January 25, 2022 07:32
-
-
Save kotay/05615286b1727169c96d to your computer and use it in GitHub Desktop.
Heroku to Logstalgia formatter
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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works like a charm. Thanks!