-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_log.rb
38 lines (34 loc) · 970 Bytes
/
access_log.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
module AccessLog
CLF_REGEXP = /
\A (?# 行頭)
(\S+)\s (?# 1 address)
(\S+)\s (?# 2 ident)
(\S+)\s (?# 3 user)
\[([^\]]+)\]\s (?# 4 time)
"(\S+)\s(\S+)\s(\S+)"\s (?# 5 6 7 method url version)
(\d+)\s (?# 8 status)
(\d+|-)\s (?# 9 bytes)
"([^"]*)"\s (?# 10 referer)
"([^"]*)" (?# 11 user_agent)
\Z (?# 行末)
/x
Entry = Struct.new( # 解析結果を保存するためのクラス
:address, :url, :version, :status, :byte,
:referer, :user_agent
)
module_function
def each_entry(file)
file.each_line do |line|
if entry = parse(line)
yield(entry)
end
end
end
def parse(line)
if m = CLF_REGEXP.match(line)
return Entry.new(*m.captures)
end
$stderr.puts("parse failure: #{line.dump}")
return nil
end
end