Class: LogRecorder
- Inherits:
-
Object
- Object
- LogRecorder
- Defined in:
- lib/log_recorder/lib/log_recorder.rb
Overview
Use this custom format (we do not record the logname or user): LogFormat “%v %h %%H:%M:%S %zt %T %>s %b %m "%U" "%Refereri" "%User-agenti" "%r"” zenaLog
Defined Under Namespace
Classes: FormatError
Constant Summary collapse
- MONTH_MAP =
{ 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12 }
Instance Method Summary collapse
-
#initialize(vhost_name, config) ⇒ LogRecorder
constructor
A new instance of LogRecorder.
-
#insert_combined_record(rec) ⇒ Object
Insert a record in the form “%h %l %u %t "%r" %>s %b "%Refereri" "%User-agenti"” %h %t %T %>s %b %m %v %h "%U" "%Refereri" "%User-agenti" "%r" 127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] “GET /apache_pb.gif HTTP/1.0” 200 2326 “www.example.com/start.html” “Mozilla/4.08 [en] (Win98; I ;Nav)”.
- #parse_date(eng_date) ⇒ Object
- #parse_path(request) ⇒ Object
- #parse_record(rec) ⇒ Object
- #test ⇒ Object
Constructor Details
#initialize(vhost_name, config) ⇒ LogRecorder
Returns a new instance of LogRecorder.
29 30 31 32 33 34 |
# File 'lib/log_recorder/lib/log_recorder.rb', line 29 def initialize(vhost_name, config) @vhost_name = vhost_name @mysql = Mysql.init @mysql.real_connect(config['host'], config['username'], config['password'], config['database'], config['port'], config['socket']) config['password'] = nil # do not keep in memory end |
Instance Method Details
#insert_combined_record(rec) ⇒ Object
Insert a record in the form “%h %l %u %t "%r" %>s %b "%Refereri" "%User-agenti"” %h %t %T %>s %b %m %v %h "%U" "%Refereri" "%User-agenti" "%r" 127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] “GET /apache_pb.gif HTTP/1.0” 200 2326 “www.example.com/start.html” “Mozilla/4.08 [en] (Win98; I ;Nav)”
40 41 42 43 44 45 46 47 48 |
# File 'lib/log_recorder/lib/log_recorder.rb', line 40 def insert_combined_record(rec) remote_host,duno,user,date,request,status,size,referer,agent = parse_record(rec) date = parse_date(date) verb, path = parse_path(request) lang, zip, mode, format = get_parameters(path) puts [remote_host,duno,user,date,[lang,zip,mode,format],status,size,referer,agent].inspect end |
#parse_date(eng_date) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/log_recorder/lib/log_recorder.rb', line 61 def parse_date(eng_date) if eng_date =~ /(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+) ([+-])(\d{2})(\d{2})/ match,d,m,y,h,min,s,ds,delta_h,delta_m = *($~.to_a) m = MONTH_MAP[m] time = Time.utc(y.to_i,m,d.to_i,h.to_i,min.to_i,s.to_i) time += (ds == '+' ? 1 : -1) * (delta_h.to_i * 3600 + delta_m.to_i * 60) else # bad raise FormatError.new("could not parse date from #{eng_date.inspect}") end return time.strftime("%Y-%m-%d %H:%M:%S") end |
#parse_path(request) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/log_recorder/lib/log_recorder.rb', line 74 def parse_path(request) if request =~ /\A(\w+) ([^ ]+)/ return [$1, $2] else # bad.. raise FormatError.new("could not parse path from #{request.inspect}") end end |
#parse_record(rec) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/log_recorder/lib/log_recorder.rb', line 50 def parse_record(rec) # %v %h %{%Y-%m-%d %H:%M:%S %z}t %T %>s %b %m \"%U\" \"%{Referer}i\" \"%{User-agent}i\" \"%r\" # FIX ... if rec =~ /\A([\d\.]+) (\S+) (\S+) \[([\w:\/]+\s[+\-]\d{4})\] "([^"]+)" (\d{3}) (\d+) "([^"]+)" "([^"]+)"/ return $~.to_a[1..-1] else # bad raise FormatError.new("could not parse record from #{rec.inspect}") end end |
#test ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/log_recorder/lib/log_recorder.rb', line 84 def test parts = parse_record('127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/4.08 [en] (Win98; I ;Nav)"') puts parts.inspect puts parse_date("10/Oct/2000:13:55:36 +0100") puts parse_date("10/Oct/2000:13:55:36 -0100") puts parse_path("GET /apache_pb.gif HTTP/1.0") puts get_site_id("test.host").inspect puts find_zip_by_path("projects-list/Clean-Water-project") insert_combined_record('213.3.85.165 - - [29/Feb/2008:11:34:22 +0100] "GET /en/image133_tiny.jpg HTTP/1.1" 200 4039 "http://zenadmin.org/en" "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr) AppleWebKit/419.3 (KHTML, like Gecko) Safari/419.3"') insert_combined_record('213.3.85.165 - - [29/Feb/2008:11:38:09 +0100] "GET /fr/projects-list/Clean-Water-project.html HTTP/1.1" 200 4760 "http://zenadmin.org/en" "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr) AppleWebKit/419.3 (KHTML, like Gecko) Safari/419.3"') end |