cloudwatchlogger 0.3.0 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c2af49513f0c3aa7ab58d1dd9118018ca1adec7fb1335493e0fad7a0a60b6e2
4
- data.tar.gz: 3ef0b93addf793462ceec0052acb7c4e13e9fe5e0c59dc9034bb4f1acd1ea5af
3
+ metadata.gz: 92d1bca725f78cc799cd9286809047cf057b2d65ba1f4819d8e7704ca042f912
4
+ data.tar.gz: 49e1294daaa98bac943f4879a783162836eff76bffc90812aad6536aa1fb44ba
5
5
  SHA512:
6
- metadata.gz: f3d46c5fe91f1eeb9c2f751263f86bd71194c6d0d8154b15fbe777baa79491aabc297289d245a50eac89fef2bbe661c0d4328eebb0c08171602d31e9c617c06f
7
- data.tar.gz: 5d71a3e18b6ee0557710e96b19b6b40e2fc7458cbef0f1906c01629bcd207b48e11e8d03e9d597a8c67160d63937111d2c82bfe7355135d54c4c9d1c3accffe0
6
+ metadata.gz: 6a3b754db960b079cc3e340875ba0902bd6be22d3d3c029177946de85ac9ace921ef7ff47c014d762c3605f97679476eb1ecb99d3d5297d33065c7c8bc351f1f
7
+ data.tar.gz: e9fc349e6b9935b1db92b21d32d4bdddfebb45d6546493e6a1b6fe9b81bccea1c088301a66222a170dd16575e2fd63d38cb7cc0204e3b71c979e212a038d7ba2
data/README.md CHANGED
@@ -29,12 +29,36 @@ log = CloudWatchLogger.new({
29
29
  }, 'YOUR_CLOUDWATCH_LOG_GROUP', 'YOUR_CLOUDWATCH_LOG_STREAM', region: 'YOUR_CLOUDWATCH_REGION' )
30
30
  ```
31
31
 
32
- Provding an empty hash instead of credentials will cause the AWS SDK to search the default credential provider chain for credentials, namely:
32
+ Providing an empty hash instead of credentials will cause the AWS SDK to search the default credential provider chain for credentials, namely:
33
33
 
34
34
  1. Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
35
35
  1. Amazon ECS container credentials (task role)
36
36
  1. Instance profile credentials (IAM role)
37
37
 
38
+ Besides the AWS region, you can also specify some other configuration options for your logger, such as:
39
+
40
+ | Property | Description |
41
+ |-----------------|--------------------------------------------------------------------------------------|
42
+ | `region` | AWS region. |
43
+ | `format` | The output format of your messages. `:json` generates JSON logs for hashed messages. |
44
+ | `open_timeout` | The open timeout in seconds. Defaults to 120. |
45
+ | `read_timeout` | The read timeout in seconds. Defaults to 120. |
46
+
47
+ This way, you could have something like this:
48
+
49
+ ```ruby
50
+ log = CloudWatchLogger.new({
51
+ access_key_id: 'YOUR_ACCESS_KEY_ID',
52
+ secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
53
+ }, 'YOUR_CLOUDWATCH_LOG_GROUP', 'YOUR_CLOUDWATCH_LOG_STREAM',
54
+ {
55
+ region: 'YOUR_CLOUDWATCH_REGION',
56
+ format: :json
57
+ })
58
+ ```
59
+
60
+
61
+
38
62
  ### With Rails
39
63
 
40
64
  config/environments/production.rb
@@ -86,6 +110,18 @@ Will produce the following log message in CloudWatch Logs:
86
110
  "<Date> severity=WARN, boom=box, bar=soap"
87
111
  ```
88
112
 
113
+ ### Custom Log Formatters
114
+ The default formatter in this gem ensures that the timestamp sent to cloudwatch will reflect the time the message was pushed onto the queue. If you want to use a custom log formatter, in order for you to not have a disparity between your actual log time and the time reflected in CloudWatch, you will need to ensure your formatter is a `Hash` with a key for `message` which will contain your log message, and `epoch_time` which should be an epoch time formatted timestamp for your log entry, like so:
115
+
116
+ ```ruby
117
+ logger.formatter = proc do |severity, datetime, progname, msg|
118
+ {
119
+ message: "CUSTOM FORMATTER PREFIX: #{msg}\n",
120
+ epoch_time: (datetime.utc.to_f.round(3) * 1000).to_i
121
+ }
122
+ end
123
+ ```
124
+
89
125
  Releasing
90
126
  -----
91
127
 
@@ -57,10 +57,7 @@ module CloudWatchLogger
57
57
  event = {
58
58
  log_group_name: @log_group_name,
59
59
  log_stream_name: @log_stream_name,
60
- log_events: [{
61
- timestamp: message_object[:epoch_time],
62
- message: message_object[:message]
63
- }]
60
+ log_events: [log_event(message_object)]
64
61
  }
65
62
  event[:sequence_token] = @sequence_token if @sequence_token
66
63
  response = @client.put_log_events(event)
@@ -94,8 +91,9 @@ module CloudWatchLogger
94
91
 
95
92
  def connect!(opts = {})
96
93
  args = { http_open_timeout: opts[:open_timeout], http_read_timeout: opts[:read_timeout] }
94
+ args[:logger] = @opts[:logger] if @opts[:logger]
97
95
  args[:region] = @opts[:region] if @opts[:region]
98
- args.merge( @credentials.key?(:access_key_id) ? { access_key_id: @credentials[:access_key_id], secret_access_key: @credentials[:secret_access_key] } : {} )
96
+ args.merge!( @credentials.key?(:access_key_id) ? { access_key_id: @credentials[:access_key_id], secret_access_key: @credentials[:secret_access_key] } : {} )
99
97
 
100
98
  @client = Aws::CloudWatchLogs::Client.new(args)
101
99
  begin
@@ -108,10 +106,22 @@ module CloudWatchLogger
108
106
  log_group_name: @log_group_name
109
107
  )
110
108
  retry
111
- rescue Aws::CloudWatchLogs::Errors::ResourceAlreadyExistsException,
109
+ rescue Aws::CloudWatchLogs::Errors::ResourceAlreadyExistsException,
112
110
  Aws::CloudWatchLogs::Errors::AccessDeniedException
113
111
  end
114
112
  end
113
+
114
+ def log_event(message_object)
115
+ timestamp = (Time.now.utc.to_f.round(3) * 1000).to_i
116
+ message = message_object
117
+
118
+ if message_object.is_a?(Hash) && %i[epoch_time message].all?{ |s| message_object.key?(s) }
119
+ timestamp = message_object[:epoch_time]
120
+ message = message_object[:message]
121
+ end
122
+
123
+ { timestamp: timestamp, message: message }
124
+ end
115
125
  end
116
126
  end
117
127
  end
@@ -1,3 +1,3 @@
1
1
  module CloudWatchLogger
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudwatchlogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zane Shannon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-23 00:00:00.000000000 Z
11
+ date: 2023-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uuid
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  - !ruby/object:Gem::Version
87
87
  version: 1.3.6
88
88
  requirements: []
89
- rubygems_version: 3.0.3
89
+ rubygems_version: 3.0.3.1
90
90
  signing_key:
91
91
  specification_version: 4
92
92
  summary: Amazon CloudWatch Logs compatiable logger for ruby.