Class: Awful::CloudWatchLogs
- Inherits:
-
Cli
show all
- Defined in:
- lib/awful/cloudwatch_logs.rb
Instance Method Summary
collapse
Methods inherited from Cli
#initialize, #ll, #version
Constructor Details
This class inherits a constructor from Awful::Cli
Instance Method Details
#create(name) ⇒ Object
48
49
50
|
# File 'lib/awful/cloudwatch_logs.rb', line 48
def create(name)
logs.create_log_group(log_group_name: name)
end
|
#delete(name) ⇒ Object
53
54
55
|
# File 'lib/awful/cloudwatch_logs.rb', line 53
def delete(name)
logs.delete_log_group(log_group_name: name)
end
|
#events(group, nth = 0) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/awful/cloudwatch_logs.rb', line 109
def events(group, nth = 0)
opt = options.dup
opt[:stream] ||= latest_stream(group, nth).log_stream_name
if opt[:tail]
opt[:head] = false
opt[:pages] = 1
opt[:limit] = opt[:tail]
end
next_token = nil
events = []
pages = 0
loop do
response = logs.get_log_events(
log_group_name: group,
log_stream_name: opt[:stream],
start_from_head: opt[:head],
limit: opt[:limit],
next_token: next_token,
)
break if response.events.count == 0 events = events + response.events
pages += 1
break if pages >= opt[:pages]
next_token = response.next_backward_token
break if next_token.nil? end
events.output do |ev|
if options[:long]
print_table ev.map { |e| [human_time(e.timestamp), e.message] }
else
puts ev.map(&:message)
end
end
end
|
#filter(group, *streams) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/awful/cloudwatch_logs.rb', line 155
def filter(group, *streams)
start_time = options[:start] ? Time.parse(options[:start]).to_i*1000 : nil
end_time = options[:end] ? Time.parse(options[:end]).to_i*1000 : nil
token = nil
loop do
resp = logs.filter_log_events(
log_group_name: group,
log_stream_names: streams.empty? ? nil : streams,
next_token: token,
start_time: start_time,
end_time: end_time,
filter_pattern: options[:pattern],
)
resp.events.each do |e|
time = set_color(human_time(e.timestamp).utc, :green)
stream = set_color(e.log_stream_name, :blue)
puts("#{time} #{stream} #{e.message}")
end
token = resp.next_token
break unless token
end
end
|
#head(group, stream) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/awful/cloudwatch_logs.rb', line 212
def head(group, stream)
out = options[:timestamp] ? ->(e) { puts("#{set_color(human_time(e.timestamp).utc, :green)} #{e.message}") } : ->(e) { puts e.message }
logs.get_log_events(
log_group_name: group,
log_stream_name: stream,
limit: options[:numlines],
start_from_head: true,
).events.each do |e|
out.call(e)
end
end
|
#latest(group, n = 0) ⇒ Object
96
97
98
99
100
|
# File 'lib/awful/cloudwatch_logs.rb', line 96
def latest(group, n = 0)
latest_stream(group, n).output do |stream|
puts stream.log_stream_name
end
end
|
#ls(prefix = nil) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/awful/cloudwatch_logs.rb', line 28
def ls(prefix = nil)
paginate(:log_groups) do |token|
logs.describe_log_groups(log_group_name_prefix: prefix, next_token: token)
end.output do |groups|
if options[:long]
print_table groups.map { |group|
[
group.log_group_name,
group.retention_in_days,
human_time(group.creation_time),
group.stored_bytes,
]
}
else
puts groups.map(&:log_group_name)
end
end
end
|
#streams(group, prefix = nil) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/awful/cloudwatch_logs.rb', line 61
def streams(group, prefix = nil)
paginate(:log_streams) do |token|
logs.describe_log_streams(
log_group_name: group,
log_stream_name_prefix: prefix,
order_by: options[:alpha] ? 'LogStreamName' : 'LastEventTime',
descending: (not options[:alpha]), limit: options[:limit],
next_token: token,
)
end.output do |streams|
if options[:long]
print_table streams.map { |s|
[ s.log_stream_name, human_time(s.creation_time), human_time(s.last_event_timestamp) ]
}
else
puts streams.map(&:log_stream_name)
end
end
end
|
#tail(group, stream) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/awful/cloudwatch_logs.rb', line 183
def tail(group, stream)
trap('SIGINT', 'EXIT')
out = if options[:timestamp]
->(e) { puts("#{set_color(human_time(e.timestamp).utc, :green)} #{e.message}") }
else
->(e) { puts e.message }
end
token = nil
loop do
resp = logs.get_log_events(
log_group_name: group,
log_stream_name: stream,
limit: options[:numlines],
next_token: token,
)
resp.events.each do |e|
out.call(e)
end
token = resp.next_forward_token
options[:follow] ? sleep(options[:sleep]) : break
end
end
|