cloudwatchlogger 0.2.1 → 1.0.1
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 +5 -5
- data/README.md +19 -1
- data/cloudwatchlogger.gemspec +2 -2
- data/lib/cloudwatchlogger/client/aws_sdk/threaded.rb +22 -14
- data/lib/cloudwatchlogger/client.rb +12 -3
- data/lib/cloudwatchlogger/version.rb +1 -1
- data/lib/cloudwatchlogger.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 132c7aab13981c643838904bd543fd3dadf18e35a46df7aa3cfba528e5732a7a
|
|
4
|
+
data.tar.gz: 7f64be893d988a05eb5bf4b607261a95f4ff623fe73d13f09e573950b1cf9e9c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1a81c48830dec6cfdb4ab851121f3cce19e2859d9549ac853338575f6e15cdadc1c03d5fa3beda5e04e68f76afdcb5ff3f00efb9ef1a0f65e3a454ec9992b841
|
|
7
|
+
data.tar.gz: cb7283ed4226ad1deda01e16a4c1578297f6f416f8dc827a76cf8edbaf8e79e389e3af9d32067cd5ed1593f0f0374b70f9e7826fd25863ed12a017bc402f0dff
|
data/README.md
CHANGED
|
@@ -20,7 +20,7 @@ log = CloudWatchLogger.new({access_key_id: 'YOUR_ACCESS_KEY_ID', secret_access_k
|
|
|
20
20
|
log.info("Hello World from Ruby")
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
In case you need to pass different region or group's different Log Stream name:
|
|
23
|
+
The region will default to the value of the environment variable `AWS_REGION`. In case you need to pass different region or group's different Log Stream name:
|
|
24
24
|
|
|
25
25
|
```ruby
|
|
26
26
|
log = CloudWatchLogger.new({
|
|
@@ -29,6 +29,12 @@ 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:
|
|
33
|
+
|
|
34
|
+
1. Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
|
|
35
|
+
1. Amazon ECS container credentials (task role)
|
|
36
|
+
1. Instance profile credentials (IAM role)
|
|
37
|
+
|
|
32
38
|
### With Rails
|
|
33
39
|
|
|
34
40
|
config/environments/production.rb
|
|
@@ -80,6 +86,18 @@ Will produce the following log message in CloudWatch Logs:
|
|
|
80
86
|
"<Date> severity=WARN, boom=box, bar=soap"
|
|
81
87
|
```
|
|
82
88
|
|
|
89
|
+
### Custom Log Formatters
|
|
90
|
+
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:
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
logger.formatter = proc do |severity, datetime, progname, msg|
|
|
94
|
+
{
|
|
95
|
+
message: "CUSTOM FORMATTER PREFIX: #{msg}\n",
|
|
96
|
+
epoch_time: (datetime.utc.to_f.round(3) * 1000).to_i
|
|
97
|
+
}
|
|
98
|
+
end
|
|
99
|
+
```
|
|
100
|
+
|
|
83
101
|
Releasing
|
|
84
102
|
-----
|
|
85
103
|
|
data/cloudwatchlogger.gemspec
CHANGED
|
@@ -5,10 +5,10 @@ Gem::Specification.new do |s|
|
|
|
5
5
|
s.name = 'cloudwatchlogger'
|
|
6
6
|
s.version = CloudWatchLogger::VERSION
|
|
7
7
|
s.date = Time.now
|
|
8
|
-
s.summary = '
|
|
8
|
+
s.summary = 'Amazon CloudWatch Logs compatiable logger for ruby.'
|
|
9
9
|
s.description = 'Logger => CloudWatchLogs'
|
|
10
10
|
|
|
11
|
-
s.license = "
|
|
11
|
+
s.license = "MIT"
|
|
12
12
|
|
|
13
13
|
s.authors = ["Zane Shannon"]
|
|
14
14
|
s.email = 'z@zcs.me'
|
|
@@ -50,17 +50,14 @@ module CloudWatchLogger
|
|
|
50
50
|
loop do
|
|
51
51
|
connect!(opts) if @client.nil?
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
break if
|
|
53
|
+
message_object = @queue.pop
|
|
54
|
+
break if message_object == :__delivery_thread_exit_signal__
|
|
55
55
|
|
|
56
56
|
begin
|
|
57
57
|
event = {
|
|
58
58
|
log_group_name: @log_group_name,
|
|
59
59
|
log_stream_name: @log_stream_name,
|
|
60
|
-
log_events: [
|
|
61
|
-
timestamp: (Time.now.utc.to_f.round(3) * 1000).to_i,
|
|
62
|
-
message: msg
|
|
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)
|
|
@@ -93,13 +90,11 @@ module CloudWatchLogger
|
|
|
93
90
|
end
|
|
94
91
|
|
|
95
92
|
def connect!(opts = {})
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
http_read_timeout: opts[:read_timeout]
|
|
102
|
-
)
|
|
93
|
+
args = { http_open_timeout: opts[:open_timeout], http_read_timeout: opts[:read_timeout] }
|
|
94
|
+
args[:region] = @opts[:region] if @opts[:region]
|
|
95
|
+
args.merge!( @credentials.key?(:access_key_id) ? { access_key_id: @credentials[:access_key_id], secret_access_key: @credentials[:secret_access_key] } : {} )
|
|
96
|
+
|
|
97
|
+
@client = Aws::CloudWatchLogs::Client.new(args)
|
|
103
98
|
begin
|
|
104
99
|
@client.create_log_stream(
|
|
105
100
|
log_group_name: @log_group_name,
|
|
@@ -110,8 +105,21 @@ module CloudWatchLogger
|
|
|
110
105
|
log_group_name: @log_group_name
|
|
111
106
|
)
|
|
112
107
|
retry
|
|
113
|
-
rescue Aws::CloudWatchLogs::Errors::ResourceAlreadyExistsException
|
|
108
|
+
rescue Aws::CloudWatchLogs::Errors::ResourceAlreadyExistsException,
|
|
109
|
+
Aws::CloudWatchLogs::Errors::AccessDeniedException
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def log_event(message_object)
|
|
114
|
+
timestamp = (Time.now.utc.to_f.round(3) * 1000).to_i
|
|
115
|
+
message = message_object
|
|
116
|
+
|
|
117
|
+
if message_object.is_a?(Hash) && %i[epoch_time message].all?{ |s| message_object.key?(s) }
|
|
118
|
+
timestamp = message_object[:epoch_time]
|
|
119
|
+
message = message_object[:message]
|
|
114
120
|
end
|
|
121
|
+
|
|
122
|
+
{ timestamp: timestamp, message: message }
|
|
115
123
|
end
|
|
116
124
|
end
|
|
117
125
|
end
|
|
@@ -33,11 +33,11 @@ module CloudWatchLogger
|
|
|
33
33
|
end.join(', ')
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
def formatter
|
|
36
|
+
def formatter(format=nil)
|
|
37
37
|
proc do |severity, datetime, progname, msg|
|
|
38
38
|
processid = Process.pid
|
|
39
|
-
if
|
|
40
|
-
MultiJson.dump(msg.merge(severity: severity,
|
|
39
|
+
if format == :json && msg.is_a?(Hash)
|
|
40
|
+
message = MultiJson.dump(msg.merge(severity: severity,
|
|
41
41
|
datetime: datetime,
|
|
42
42
|
progname: progname,
|
|
43
43
|
pid: processid))
|
|
@@ -45,6 +45,11 @@ module CloudWatchLogger
|
|
|
45
45
|
message = "#{datetime} "
|
|
46
46
|
message << massage_message(msg, severity, processid)
|
|
47
47
|
end
|
|
48
|
+
|
|
49
|
+
{
|
|
50
|
+
message: message,
|
|
51
|
+
epoch_time: epoch_from(datetime)
|
|
52
|
+
}
|
|
48
53
|
end
|
|
49
54
|
end
|
|
50
55
|
|
|
@@ -80,6 +85,10 @@ module CloudWatchLogger
|
|
|
80
85
|
uuid = UUID.new
|
|
81
86
|
@log_stream_name ||= "#{Socket.gethostname}-#{uuid.generate}"
|
|
82
87
|
end
|
|
88
|
+
|
|
89
|
+
def epoch_from(datetime)
|
|
90
|
+
(datetime.utc.to_f.round(3) * 1000).to_i
|
|
91
|
+
end
|
|
83
92
|
end
|
|
84
93
|
end
|
|
85
94
|
end
|
data/lib/cloudwatchlogger.rb
CHANGED
|
@@ -11,7 +11,7 @@ module CloudWatchLogger
|
|
|
11
11
|
logger = Logger.new(client)
|
|
12
12
|
|
|
13
13
|
if client.respond_to?(:formatter)
|
|
14
|
-
logger.formatter = client.formatter
|
|
14
|
+
logger.formatter = client.formatter(opts[:format])
|
|
15
15
|
elsif client.respond_to?(:datetime_format)
|
|
16
16
|
logger.datetime_format = client.datetime_format
|
|
17
17
|
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.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Zane Shannon
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-12-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: uuid
|
|
@@ -69,7 +69,7 @@ files:
|
|
|
69
69
|
- lib/cloudwatchlogger/version.rb
|
|
70
70
|
homepage: http://github.com/zshannon/cloudwatchlogger
|
|
71
71
|
licenses:
|
|
72
|
-
-
|
|
72
|
+
- MIT
|
|
73
73
|
metadata: {}
|
|
74
74
|
post_install_message:
|
|
75
75
|
rdoc_options: []
|
|
@@ -86,9 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
86
86
|
- !ruby/object:Gem::Version
|
|
87
87
|
version: 1.3.6
|
|
88
88
|
requirements: []
|
|
89
|
-
|
|
90
|
-
rubygems_version: 2.6.11
|
|
89
|
+
rubygems_version: 3.2.3
|
|
91
90
|
signing_key:
|
|
92
91
|
specification_version: 4
|
|
93
|
-
summary:
|
|
92
|
+
summary: Amazon CloudWatch Logs compatiable logger for ruby.
|
|
94
93
|
test_files: []
|