Repository navigation
Expand file tree
/
Copy pathlog_parser.rb
More file actions
92 lines (76 loc) · 1.96 KB
/
Copy pathlog_parser.rb
File metadata and controls
92 lines (76 loc) · 1.96 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# A simple, yet powerful log parser
#
# Dynamically supports any log format; just define needed format:
# parser = Parser.new filename do
# field :datetime
# field :url
# field :referer do |value|
# value == "-" ? "N/A" : value
# end
# end
#
# Parses each line to a structured hash; following the structure above:
# {datetime: "...", url: "...", referer: "..."}
#
# It implements Enumerable, so you have everything Enumerator offers:
# (each, select, inject, etc..)
# parser.each { |h| puts h }
#
# AND - it supports a lazy enumerator! so you can do crazy stuff with
# a big size log file:
# e = parser.lazy.select { |x| x[:datetime] > 3.days.ago }.select { |x| x[:referer] = "google.com" }
# e.next # => will return the next matching log line
# e.next # => will return the next matching log line
# e.next ...
class Parser
include Enumerable
attr_reader :fields
def initialize(filename, &block)
@fields = []
@file = File.open filename
self.instance_eval(&block) if block_given?
end
def field(name, &block)
@fields << [name, (block_given? ? block : proc { |value| value })]
end
def each
@file.each do |line|
yield parse_line(line)
end
end
def close
@file.close
end
private
def parse_line(line)
line.split(" ").zip(@fields).inject({}) do |h, f|
value = f.first
key = f.last.first
p = f.last.last
h[key] = p.call(value)
h
end
end
end
# usage
patriarchs = proc do
field :name do |value|
value.capitalize
end
field :age do |value|
value == "-" ? -1 : value.to_i
end
field :gender
field :spouse
end
parser = Parser.new "log.log", &patriarchs
# lazy parse of log
parser.lazy.select {|x| x[:gender] == "male"}.select { |x| x[:age] > 150 }.to_a
parser.lazy.select { |x| x[:gender] == "male" }.select { |x| x[:spouse] =~ /leah/ }.to_a
a = parser.lazy.select { |x| x[:gender] == "female" }.select { |x| x[:spouse] =~ /jacob/ }
a.next
# eager parse of log
parser.each do |h|
puts h
end
parser.close