apache_log-parser 3.1.1 → 3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/apache_log/parser.rb +21 -26
- data/lib/apache_log/parser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79f05f655abf68de378abfbc46727b3ffb63c814
|
4
|
+
data.tar.gz: 881391956529b0d466b71b2ead3118a9586da5b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02cc9b7d20474c3b0d3049e36c7f1e8e4f6e72374457e002132cc52156bf22f3a062c18d780131aa021ab877c2779f433e119ffa269d89fcc9b0a4019b2a35df
|
7
|
+
data.tar.gz: e5b3c7f417621295ef82346f919d900f5ef56b54e49faede00373bb21bea80462790145b53b040627e43ce1b9abb1cfa23266a01775c583b63473395732b2978
|
data/lib/apache_log/parser.rb
CHANGED
@@ -3,25 +3,24 @@ require 'date'
|
|
3
3
|
|
4
4
|
module ApacheLog
|
5
5
|
class Parser
|
6
|
-
|
7
|
-
|
8
|
-
combined_fields = common_fields + %w(referer user_agent)
|
6
|
+
COMMON_FIELDS = %w(remote_host identity_check user datetime request status size)
|
7
|
+
COMBINED_FIELDS = COMMON_FIELDS + %w(referer user_agent)
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
COMMON_PATTERN = '(?:^|\s)((?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:[\w:]+?))\s+(\S+)\s+(\S+)\s+\[(\d{2}\/.*\d{4}:\d{2}:\d{2}:\d{2}\s.*)\]\s+"(.*?)"\s+(\S+)\s+(\S+)'
|
10
|
+
COMBINED_PATTERN = COMMON_PATTERN + '\s+"(.*?[^\\\\])"\s+"(.*?[^\\\\])"'
|
11
|
+
ADDITIONAL_PATTERN = '\s+"?([^"]*)"?'
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def initialize(format, additional_fields=[])
|
14
|
+
additional_pattern = ''
|
15
|
+
additional_pattern << ADDITIONAL_PATTERN * additional_fields.size
|
17
16
|
|
18
17
|
case format
|
19
18
|
when 'common'
|
20
|
-
@fields =
|
21
|
-
@pattern = /#{
|
19
|
+
@fields = COMMON_FIELDS + additional_fields
|
20
|
+
@pattern = /#{COMMON_PATTERN}#{additional_pattern}/
|
22
21
|
when 'combined'
|
23
|
-
@fields =
|
24
|
-
@pattern = /#{
|
22
|
+
@fields = COMBINED_FIELDS + additional_fields
|
23
|
+
@pattern = /#{COMBINED_PATTERN}#{additional_pattern}/
|
25
24
|
else
|
26
25
|
raise "format error\n no such format: <#{format}> \n"
|
27
26
|
end
|
@@ -29,28 +28,27 @@ module ApacheLog
|
|
29
28
|
|
30
29
|
def parse(line)
|
31
30
|
matched = @pattern.match(line)
|
31
|
+
|
32
32
|
raise "parse error\n at line: <#{line}> \n" if matched.nil?
|
33
|
+
|
33
34
|
generate_hash(@fields, matched.to_a)
|
34
35
|
end
|
35
36
|
|
36
37
|
private
|
37
38
|
|
38
39
|
def generate_hash(keys, values)
|
39
|
-
|
40
|
-
|
41
|
-
keys.each.with_index(1) do |key, idx|
|
40
|
+
keys.each.with_index(1).each_with_object({}) do |(key, i), hash|
|
42
41
|
key = key.to_sym
|
42
|
+
|
43
43
|
case key
|
44
44
|
when :datetime
|
45
|
-
hash[key] = to_datetime(values[
|
45
|
+
hash[key] = to_datetime(values[i])
|
46
46
|
when :request
|
47
|
-
hash[key] = parse_request(values[
|
47
|
+
hash[key] = parse_request(values[i])
|
48
48
|
else
|
49
|
-
hash[key] = values[
|
49
|
+
hash[key] = values[i]
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
53
|
-
hash
|
54
52
|
end
|
55
53
|
|
56
54
|
def to_datetime(str)
|
@@ -59,11 +57,8 @@ module ApacheLog
|
|
59
57
|
|
60
58
|
def parse_request(str)
|
61
59
|
method, path, protocol = str.split
|
62
|
-
|
63
|
-
|
64
|
-
path: path,
|
65
|
-
protocol: protocol,
|
66
|
-
}
|
60
|
+
|
61
|
+
{ method: method, path: path, protocol: protocol }
|
67
62
|
end
|
68
63
|
end
|
69
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apache_log-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.
|
4
|
+
version: 3.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuichi Takada
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|