log_slice 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/log_slice_date +19 -11
- data/lib/log_slice/date_range.rb +37 -0
- data/lib/log_slice.rb +1 -0
- data/log_slice.gemspec +1 -1
- data/spec/helper.rb +1 -1
- data/spec/log_slice_date_range_spec.rb +48 -0
- metadata +5 -3
data/bin/log_slice_date
CHANGED
@@ -1,17 +1,25 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'date'
|
3
3
|
require File.expand_path("../lib/log_slice", File.dirname(__FILE__))
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
$stderr.puts "
|
8
|
-
$stderr.puts "EXAMPLE: #{$0} '2012-08-31 12:00:00' /tmp/something.log\n\n"
|
4
|
+
if ARGV.length < 2
|
5
|
+
$stderr.puts "USAGE: #{$0} DATE [END_DATE] FILEPATH\n\n"
|
6
|
+
$stderr.puts "EXAMPLE: #{$0} '2012-08-31 12:00:00' /tmp/something.log\n"
|
7
|
+
$stderr.puts "EXAMPLE: #{$0} '2012-08-31 12:00:00' '2012-08-31 12:05:00' /tmp/something.log\n"
|
9
8
|
$stderr.puts "Expects log lines to contain a DateTime string enclosed in [square brackets] \n\n"
|
10
|
-
$stderr.puts "Could not parse date argument\n\n"
|
11
|
-
raise
|
12
9
|
end
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
|
11
|
+
start_date = ARGV[0]
|
12
|
+
(end_date, file) = begin
|
13
|
+
if ARGV.length == 2
|
14
|
+
[nil, ARGV[1]]
|
15
|
+
else
|
16
|
+
[ARGV[1], ARGV[2]]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
LogSlice::DateRange.new(file, start_date, end_date).each do |line|
|
22
|
+
puts line
|
23
|
+
end
|
24
|
+
rescue Errno::EPIPE
|
16
25
|
end
|
17
|
-
puts file.readline until file.eof?
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class LogSlice
|
4
|
+
class DateRange
|
5
|
+
|
6
|
+
# @param file [File, String]
|
7
|
+
# @param start_date [DateTime, String]
|
8
|
+
# @param end_date [DateTime, String, nil]
|
9
|
+
def initialize file, start_date, end_date=nil
|
10
|
+
@file = file
|
11
|
+
@start_date = start_date.is_a?(DateTime) ? start_date : DateTime.parse(start_date)
|
12
|
+
@end_date = (end_date.is_a?(DateTime) || end_date.nil?) ? end_date : DateTime.parse(end_date)
|
13
|
+
end
|
14
|
+
|
15
|
+
def each
|
16
|
+
file = LogSlice.new(@file).find do |line|
|
17
|
+
date_compare @start_date, line
|
18
|
+
end
|
19
|
+
begin
|
20
|
+
line = file.readline
|
21
|
+
if @end_date.nil? || date_compare(@end_date, line) == 1
|
22
|
+
yield line
|
23
|
+
else
|
24
|
+
break
|
25
|
+
end
|
26
|
+
end until file.eof?
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def date_compare date, line
|
32
|
+
date_string = line.match(/\[([^\]]+)\]/)[1]
|
33
|
+
date <=> DateTime.parse(date_string)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/log_slice.rb
CHANGED
data/log_slice.gemspec
CHANGED
data/spec/helper.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path('../lib/log_slice', File.dirname(__FILE__))
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
describe LogSlice::DateRange do
|
5
|
+
|
6
|
+
let(:file_contents) {
|
7
|
+
%{
|
8
|
+
[2012-08-31 17:19:45] test 1
|
9
|
+
[2012-08-31 17:19:57] test 2
|
10
|
+
[2012-08-31 17:19:57] test 3
|
11
|
+
[2012-08-31 17:20:12] test 4
|
12
|
+
[2012-08-31 17:20:24] test 5
|
13
|
+
[2012-08-31 17:20:35] test 6
|
14
|
+
[2012-08-31 17:21:02] test 7
|
15
|
+
[2012-08-31 17:21:09] test 8
|
16
|
+
}.strip
|
17
|
+
}
|
18
|
+
|
19
|
+
it "works" do
|
20
|
+
start_date = '2012-08-31 17:20:00'
|
21
|
+
end_date = '2012-08-31 17:21:00'
|
22
|
+
lines = []
|
23
|
+
LogSlice::DateRange.new(string_to_file(file_contents), start_date, end_date).each do |line|
|
24
|
+
lines << line.strip
|
25
|
+
end
|
26
|
+
lines.should == [
|
27
|
+
"[2012-08-31 17:20:12] test 4",
|
28
|
+
"[2012-08-31 17:20:24] test 5",
|
29
|
+
"[2012-08-31 17:20:35] test 6"
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "works without an end date" do
|
34
|
+
start_date = '2012-08-31 17:20:00'
|
35
|
+
lines = []
|
36
|
+
LogSlice::DateRange.new(string_to_file(file_contents), start_date, nil).each do |line|
|
37
|
+
lines << line.strip
|
38
|
+
end
|
39
|
+
lines.should == [
|
40
|
+
"[2012-08-31 17:20:12] test 4",
|
41
|
+
"[2012-08-31 17:20:24] test 5",
|
42
|
+
"[2012-08-31 17:20:35] test 6",
|
43
|
+
"[2012-08-31 17:21:02] test 7",
|
44
|
+
"[2012-08-31 17:21:09] test 8"
|
45
|
+
]
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: log_slice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 4
|
9
|
+
version: "0.4"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joel Plane
|
@@ -45,9 +45,11 @@ files:
|
|
45
45
|
- README.md
|
46
46
|
- bin/log_slice_date
|
47
47
|
- lib/log_slice.rb
|
48
|
+
- lib/log_slice/date_range.rb
|
48
49
|
- lib/log_slice/search_boundary.rb
|
49
50
|
- log_slice.gemspec
|
50
51
|
- spec/helper.rb
|
52
|
+
- spec/log_slice_date_range_spec.rb
|
51
53
|
- spec/log_slice_spec.rb
|
52
54
|
homepage: https://github.com/joelplane/log_slice
|
53
55
|
licenses: []
|