Skip to content

Instantly share code, notes, and snippets.

@simple
Created August 24, 2011 08:51
Show Gist options
  • Save simple/1167611 to your computer and use it in GitHub Desktop.
Save simple/1167611 to your computer and use it in GitHub Desktop.
Preprocessing Apache Log
#!/usr/bin/env ruby
require 'date'
require 'apachelogregex'
# apache log format
parser = ApacheLogRegex.new('%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %I %b %D')
File.open(ARGV[0]).readlines.each do | line |
p = parser.parse(line)
next if p.nil?
# prepare needed columns
path = p['%r'].split(' ')[1]
# from zend log default to YYYY-MM-DD H:i:S
date = DateTime.strptime(p['%t'], '[%d/%b/%Y:%H:%M:%S %Z]').strftime('%Y-%m-%d %H:%M:%S')
puts "#{date}\t#{p['%h']}\t#{path}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment