aws-logs 0.1.0 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 56d76017d06966a77c263a2aaa65e2925882cc919e2f3baff52276b5b1f53b05
4
- data.tar.gz: 0f60ef45cdea2a00de22c924b2815f14be84fc59dd16957f527416ad58ac6eb7
3
+ metadata.gz: e2d97ba7271f44e4d07374bf86cb6925ada130f8359f4efa1c56ecb7903d7d0c
4
+ data.tar.gz: 8faef88a6dbf7c0a0ed7f9d10d2a7975fd942fb89a5fc88ea43a29cac645c0ee
5
5
  SHA512:
6
- metadata.gz: db6f055ed5ef2094493f22484bf2fa56c40977980b915583aa61c72c2e74fc8675a5c3a4992d64ec15e042aa9692eec7e0f696f57e39b3f2fd0722a79657d402
7
- data.tar.gz: e3970d0c5db5c4eb363a02333d7262a87359cd73577aad5b986fba4c78ed9d33bedb5b9becdbefe614eac554bdfa170d4405a1df0cae68d55740b627591bec9e
6
+ metadata.gz: ebcf238f59349ec1ba0abc6e4f285db9b23062f9d1d35085af59d16b976bd0ba84a0bf37740fd8726cc33fe9f8607cc286faefbd5b3d55a5134dcd30ea796f83
7
+ data.tar.gz: 27a69941be1c6b4ec8b62850eafb7764f47b3995bdabff1dd780120f9d8091c060567d2d8834b409efd71042a3fa4fa4e5b3b2fefa207c506f074e070b0cdba2
data/CHANGELOG.md CHANGED
@@ -3,5 +3,22 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.3.3]
7
+ - #2 add overlap to window to account for delayed logs received at the same time
8
+
9
+ ## [0.3.2]
10
+ - #1 clean up end_loop_signal logic
11
+
12
+ ## [0.3.1]
13
+ - fix --no-follow option
14
+
15
+ ## [0.3.0]
16
+ - display final logs upon stop_follow!
17
+
18
+ ## [0.2.0]
19
+ - add stop_follow! method
20
+ - friendly error message when log not found
21
+ - improve `@follow` default, `@log_group_name` and stdout sync true by default
22
+
6
23
  ## [0.1.0]
7
24
  - Initial release.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # AwsLogs
1
+ # aws-logs
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/GEMNAME.png)](http://badge.fury.io/rb/GEMNAME)
4
4
 
@@ -12,7 +12,7 @@ Tail AWS CloudWatch Logs.
12
12
 
13
13
  ## Examples
14
14
 
15
- Here's a couple of examples where the `LOG_GROUP=/aws/codebuild/demo`:
15
+ Here's a couple of examples where `LOG_GROUP=/aws/codebuild/demo`:
16
16
 
17
17
  aws-logs tail /aws/codebuild/demo --since 60m
18
18
  aws-logs tail /aws/codebuild/demo --since "2018-08-08 08:00:00"
@@ -25,15 +25,7 @@ Here's a couple of examples where the `LOG_GROUP=/aws/codebuild/demo`:
25
25
 
26
26
  ## Installation
27
27
 
28
- Add this line to your application's Gemfile:
29
-
30
- gem "aws-logs"
31
-
32
- And then execute:
33
-
34
- bundle
35
-
36
- Or install it yourself as:
28
+ Install with:
37
29
 
38
30
  gem install aws-logs
39
31
 
data/lib/aws_logs.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $stdout.sync = true unless ENV["AWS_LOGS_STDOUT_SYNC"] == "0"
2
+
1
3
  $:.unshift(File.expand_path("../", __FILE__))
2
4
  require "aws_logs/version"
3
5
  require "rainbow/ext/string"
data/lib/aws_logs/cli.rb CHANGED
@@ -9,8 +9,8 @@ module AwsLogs
9
9
  option :log_stream_name_prefix, desc: "Filters the results to include only events from log streams that have names starting with this prefix. Can only use log_stream_names or log_stream_name_prefix but not both."
10
10
  option :filter_pattern, desc: "The filter pattern to use. If not provided, all the events are matched"
11
11
  option :follow_until, desc: "Exit out of the follow loop once this text is found."
12
- def tail(log_group)
13
- Tail.new(options.merge(log_group: log_group)).run
12
+ def tail(log_group_name)
13
+ Tail.new(options.merge(log_group_name: log_group_name)).run
14
14
  end
15
15
 
16
16
  desc "completion *PARAMS", "Prints words for auto-completion."
@@ -41,4 +41,17 @@ Since supports these formats:
41
41
  * d - days
42
42
  * w - weeks
43
43
 
44
- Since does not current support combining the formats. IE: 5m30s.
44
+ Since does not support combining the formats. IE: 5m30s.
45
+
46
+ ## Filter Pattern
47
+
48
+ The `--filter-pattern` option is quite powerful as CloudWatch supports a full
49
+ [Filter and Pattern Syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html).
50
+
51
+ To match terms with spaces in it, you'll need quotes around it. Otherise, the match will be an OR of the terms. Example:
52
+
53
+ aws-logs tail /aws/codebuild/demo --filter-pattern '"Wed Nov 27 23"' --since 3h --no-follow
54
+
55
+ Here's an example of matching with an exclude pattern using the `-` (minus sign).
56
+
57
+ aws-logs tail /aws/codebuild/demo --filter-pattern '"ERROR" - "Exiting"' --since 3h --no-follow
data/lib/aws_logs/tail.rb CHANGED
@@ -6,7 +6,9 @@ module AwsLogs
6
6
 
7
7
  def initialize(options={})
8
8
  @options = options
9
- @log_group = options[:log_group]
9
+ @log_group_name = options[:log_group_name]
10
+ # Setting to ensure matches default CLI option
11
+ @follow = @options[:follow].nil? ? true : @options[:follow]
10
12
 
11
13
  @loop_count = 0
12
14
  @output = [] # for specs
@@ -14,15 +16,6 @@ module AwsLogs
14
16
  set_trap
15
17
  end
16
18
 
17
- @@end_loop_signal = false
18
- def set_trap
19
- Signal.trap("INT") {
20
- puts "\nCtrl-C detected. Exiting..."
21
- @@end_loop_signal = true # delayed exit, usefu control loop flow though
22
- exit # immediate exit
23
- }
24
- end
25
-
26
19
  def reset
27
20
  @events = [] # constantly replaced with recent events
28
21
  @last_shown_event_id = nil
@@ -42,14 +35,24 @@ module AwsLogs
42
35
  return
43
36
  end
44
37
 
38
+ # We overlap the sliding window because CloudWatch logs can receive or send the logs out of order.
39
+ # For example, a bunch of logs can all come in at the same second, but they haven't registered to CloudWatch logs
40
+ # yet. If we don't overlap the sliding window then we'll miss the logs that were delayed in registering.
41
+ overlap = 60*1000 # overlap the sliding window by a minute
45
42
  since, now = initial_since, current_now
46
- while true && !end_loop?
43
+ until end_loop?
47
44
  refresh_events(since, now)
48
45
  display
49
- since, now = now, current_now
46
+ since, now = now-overlap, current_now
50
47
  loop_count!
51
- sleep 5 if @options[:follow] && !@@end_loop_signal && !ENV["AWS_LOGS_TEST"]
48
+ sleep 5 if @follow && !ENV["AWS_LOGS_TEST"]
52
49
  end
50
+ # Refresh and display a final time in case the end_loop gets interrupted by stop_follow!
51
+ refresh_events(since, now)
52
+ display
53
+ rescue Aws::CloudWatchLogs::Errors::ResourceNotFoundException => e
54
+ puts "ERROR: #{e.class}: #{e.message}".color(:red)
55
+ puts "Log group #{@log_group_name} not found."
53
56
  end
54
57
 
55
58
  def refresh_events(start_time, end_time)
@@ -59,7 +62,7 @@ module AwsLogs
59
62
  # TODO: can hit throttle limit if there are lots of pages
60
63
  while next_token
61
64
  options = {
62
- log_group_name: @log_group, # required
65
+ log_group_name: @log_group_name, # required
63
66
  start_time: start_time,
64
67
  end_time: end_time,
65
68
  # limit: 10,
@@ -101,9 +104,7 @@ module AwsLogs
101
104
  return unless follow_until
102
105
 
103
106
  messages = @events.map(&:message)
104
- if messages.detect { |m| m.include?(follow_until) }
105
- @@end_loop_signal = true
106
- end
107
+ @@end_loop_signal = messages.detect { |m| m.include?(follow_until) }
107
108
  end
108
109
 
109
110
  def say(text)
@@ -114,6 +115,21 @@ module AwsLogs
114
115
  @output.join("\n") + "\n"
115
116
  end
116
117
 
118
+ def set_trap
119
+ Signal.trap("INT") {
120
+ puts "\nCtrl-C detected. Exiting..."
121
+ exit # immediate exit
122
+ }
123
+ end
124
+
125
+ # The stop_follow! results in a little waiting because it signals to break the polling loop.
126
+ # Since it's in the middle of the loop process, the loop will finish the sleep 5 first.
127
+ # So it can pause from 0-5 seconds.
128
+ @@end_loop_signal = false
129
+ def self.stop_follow!
130
+ @@end_loop_signal = true
131
+ end
132
+
117
133
  private
118
134
  def initial_since
119
135
  since = @options[:since]
@@ -136,7 +152,7 @@ module AwsLogs
136
152
 
137
153
  # Useful for specs
138
154
  def max_loop_count
139
- @options[:follow] ? nil : 1
155
+ @follow ? nil : 1
140
156
  end
141
157
  end
142
158
  end
@@ -1,3 +1,3 @@
1
1
  module AwsLogs
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-logs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2019-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport