Created
September 3, 2012 21:00
-
-
Save jonelf/3613495 to your computer and use it in GitHub Desktop.
Produces colorized output of the Common Log Format
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
# 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