Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created September 3, 2012 21:00
Show Gist options
  • Save jonelf/3613495 to your computer and use it in GitHub Desktop.
Save jonelf/3613495 to your computer and use it in GitHub Desktop.
Produces colorized output of the Common Log Format
# Produces colorized output of the Common Log Format
# Usage: $ tail -f access.log | ruby colorpipe.rb
require 'rubygems'
require 'fastercsv'
require 'colorize'
require 'resolv'
colors = String.colors.reject {|c| c == :black || c == :light_cyan}
hosts = {}
loop do
i = 1
line = $stdin.readline
arr = line.gsub(/(\[.*)( )(.*\])/, '\1\3').parse_csv(:col_sep => ' ').
map {|s| i+=1; s.colorize(colors[i]) }
if arr.count > 0
ip = arr[0].uncolorize
host = hosts.has_key?(ip) ? hosts[ip] : hosts[ip] = (Resolv.getname(ip) rescue ip)
arr[0] = host.colorize(:yellow)
puts arr.join(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment