-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-formatter.rb
54 lines (48 loc) · 1.87 KB
/
log-formatter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby
# frozen_string_literal: true
# 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('"', '')
host = parts[5].gsub('host=', '').gsub('"', '')
dyno = parts[8].gsub('dyno=', '').gsub('"', '')
status = parts[11].gsub('status=', '')
size = parts[12].gsub('bytes=', '')
# Custom Log Format:
#
# Logstalgia now supports a pipe ('|') delimited custom log file format:
#
# timestamp - unix timestamp of the request date.
# hostname - hostname of the request
# path - path requested
# response_code - the response code from the webserver (eg 200)
# response_size - the size of the response in bytes
#
# The following are optional:
# success - 1 or 0 to indicate if successful
# response_colour - response colour in hexidecial (#FFFFFF) format
# referrer url - the referrer url
# user agent - the user agent
# virtual host - the virtual host (to use with --paddle-mode vhost)
# pid - process id or some other identifier (--paddle-mode pid)
output_line = "#{timestamp}|#{ip}|#{request}|#{status}|#{size}|||||#{host}|#{dyno}"
begin
$stdout.puts output_line
rescue Errno::EPIPE
exit(74)
end
end
end