logm 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/logmerge.rb +84 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8268e85c780fcb505648e7f36ef915b0b82a253907148a2f95485a690bef294
|
4
|
+
data.tar.gz: 90042d98654e802e6c694de035297c120c2fbc74c65a0d69f95a330d3927a312
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6966a36051254dee8b55b0196250b4d682f8e136f8da76525dbab6a045953873915d541523827e74f8d6e6b7a4c0760f0758a4da94d1f2fa5ecedb953bfa340
|
7
|
+
data.tar.gz: 3d163efc23ee1868c288074ab553dbc2622fbc8b17b1401213eea97b6e7e77e4e9eddba2988de6274164931b8a2932f3698562fd118cc9f18fd143b49db3e439
|
data/lib/logmerge.rb
CHANGED
@@ -2,6 +2,8 @@ require 'colored'
|
|
2
2
|
require 'date'
|
3
3
|
require 'optparse'
|
4
4
|
|
5
|
+
|
6
|
+
|
5
7
|
class Numeric
|
6
8
|
def minutes; self/1440.0 end
|
7
9
|
alias :minute :minutes
|
@@ -13,8 +15,27 @@ class Numeric
|
|
13
15
|
alias :millisecond :milliseconds
|
14
16
|
end
|
15
17
|
|
18
|
+
class File
|
19
|
+
alias :old_initialize :initialize
|
20
|
+
alias :old_readline :readline
|
21
|
+
def initialize (*args)
|
22
|
+
@buf=[]
|
23
|
+
old_initialize(*args)
|
24
|
+
end
|
25
|
+
def unreadline (str)
|
26
|
+
@buf.push(str)
|
27
|
+
end
|
28
|
+
def readline (*args)
|
29
|
+
@buf.empty? ? old_readline(*args) : @buf.pop
|
30
|
+
end
|
31
|
+
|
32
|
+
def buf
|
33
|
+
@buf
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
16
37
|
class Log
|
17
|
-
attr_accessor :file, :line, :offet, :tag, :timestamp, :finished
|
38
|
+
attr_accessor :file, :line, :next, :offet, :tag, :timestamp, :next_timestamp, :finished
|
18
39
|
def initialize(path)
|
19
40
|
@offset = 0
|
20
41
|
if path.include?(':')
|
@@ -30,34 +51,74 @@ class Log
|
|
30
51
|
buffer
|
31
52
|
end
|
32
53
|
|
33
|
-
def parseTimestamp(
|
54
|
+
def parseTimestamp(current)
|
55
|
+
# puts "parsingTimestamp: #{current}"
|
34
56
|
# would be great to generalize this more
|
35
|
-
|
57
|
+
ts = nil
|
58
|
+
if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/
|
36
59
|
# agent logs
|
37
|
-
|
60
|
+
ts_part = current[1...24]
|
61
|
+
# puts "ts_part: #{ts_part}"
|
62
|
+
ts = DateTime.parse(ts_part)
|
38
63
|
ts = ts + @offset.seconds
|
39
|
-
|
40
|
-
else
|
64
|
+
elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/
|
41
65
|
#mms logs
|
42
|
-
|
66
|
+
ts_part = current[0...28]
|
67
|
+
# puts "ts_part: #{ts_part}"
|
68
|
+
ts = DateTime.parse(current[0...28])
|
43
69
|
ts = ts + @offset.seconds
|
44
|
-
line[0...28] = "[#{ts.to_s}]"
|
45
70
|
end
|
46
71
|
ts
|
72
|
+
rescue Date::Error
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def applyTimestamp(current, timestamp)
|
77
|
+
if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/
|
78
|
+
current[1...24] = timestamp.to_s
|
79
|
+
elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/
|
80
|
+
current[0...28] = "[#{timestamp.to_s}]"
|
81
|
+
end
|
82
|
+
current
|
83
|
+
end
|
84
|
+
|
85
|
+
def append(current, timestamp=nil)
|
86
|
+
current.prepend("[#{@tag}]") if @tag && timestamp
|
87
|
+
@line.push(current)
|
47
88
|
end
|
48
89
|
|
49
90
|
def buffer
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
91
|
+
@count ||= 0
|
92
|
+
# puts "buffer: #{@count}"
|
93
|
+
# puts "file buffer: #{file.buf}"
|
94
|
+
@count = @count + 1
|
95
|
+
# TODO: add support for multiline log statements
|
96
|
+
# basically read until next timestamp found including all lines as an entry
|
97
|
+
@line = []
|
98
|
+
@timestamp = nil
|
99
|
+
timestamp_index = 0
|
100
|
+
while @timestamp.nil? do
|
101
|
+
current = file.readline.strip
|
102
|
+
# puts "current: #{current}"
|
103
|
+
@timestamp = parseTimestamp(current)
|
104
|
+
# puts "timestamp: #{@timestamp}"
|
105
|
+
append(current, @timestamp)
|
106
|
+
timestamp_index = timestamp_index + 1 if @timestamp.nil?
|
107
|
+
end
|
108
|
+
|
109
|
+
applyTimestamp(line[timestamp_index], @timestamp) if line.size > timestamp_index
|
110
|
+
|
111
|
+
next_timestamp = nil
|
112
|
+
while next_timestamp.nil? do
|
113
|
+
current = file.readline.strip
|
114
|
+
# puts "nextcurrent: #{current}"
|
115
|
+
next_timestamp = parseTimestamp(current)
|
116
|
+
# puts "next_timestamp: #{next_timestamp}"
|
117
|
+
file.unreadline(current + "\n") && break unless next_timestamp.nil?
|
118
|
+
append(current)
|
56
119
|
end
|
57
120
|
rescue EOFError
|
58
|
-
@
|
59
|
-
@finished = true
|
60
|
-
@timestamp = nil
|
121
|
+
@finished = true if @line.empty?
|
61
122
|
end
|
62
123
|
|
63
124
|
def take
|
@@ -88,7 +149,12 @@ class LogMerge
|
|
88
149
|
index = logs.index(min_log)
|
89
150
|
color_index = index % colors.size
|
90
151
|
color = colors[color_index]
|
91
|
-
|
152
|
+
loglines = min_log.take
|
153
|
+
puts applyColor(loglines, color).join("\n")
|
92
154
|
end
|
93
155
|
end
|
156
|
+
|
157
|
+
def applyColor(loglines, color)
|
158
|
+
loglines.map{|l| Colored.colorize(l, foreground: color)}
|
159
|
+
end
|
94
160
|
end
|