ama_logger 1.1.1 → 1.2.0

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
- SHA1:
3
- metadata.gz: 97f8723f2fd607f2c2a4b6a2f9ecb7c0828f361c
4
- data.tar.gz: e35ae18d44bf2f2ad3d863e6608368c7cbe93aa4
2
+ SHA256:
3
+ metadata.gz: 82c2fc23eb3fb3b209ca6a0e5a4227f73adc9e694a4a48f20b9d830a83fe9834
4
+ data.tar.gz: 967b8acb04279d9d1abc42ef289a76dd7e9ca4681746c7be43d9615c9189540f
5
5
  SHA512:
6
- metadata.gz: 998b0a701d0988acf1bd99bc8392f94eecf233235b38a06c272c02fbc1de6125a9bd93137e37cd15e1aa243a4a1884d3252e17b477e109db6bec50de9482b594
7
- data.tar.gz: f6a072cf401c8aa6ae243e709b20c497607f55410239fb21d6b671396f8777fd4871aa016e0c5ffc137a7b8618c3dfaadd7d0c786dc367edad5a16db82309da8
6
+ metadata.gz: 3a0fdb20f027e387375528eae35f4275f9f8a2ae022e15e3e56f940236b752b8984982d5b88c07b2928ac09d8124251ad5c6a1c16661fa33a723ab6f472b0a96
7
+ data.tar.gz: 1f9ddad15968aafcb468789469cb1296374203e79c290941ef51ae305ac95d3b1ba87105a5f0d8449a0ee2bc83850ecb894e0f389675596ab957e251825b4233
@@ -17,6 +17,6 @@ Metrics/LineLength:
17
17
  Metrics/MethodLength:
18
18
  Max: 25
19
19
  Metrics/AbcSize:
20
- Max: 17
20
+ Max: 20
21
21
  Style/Lambda:
22
22
  EnforcedStyle: literal
data/README.md CHANGED
@@ -32,7 +32,7 @@ This library includes the following custom log formatters:
32
32
 
33
33
  ### Ama::Logger::Formatter::Lambda
34
34
 
35
- This formatter accepts a Ruby hash as a message and outputs a JSON string.
35
+ This formatter accepts a Ruby hash as a message, an AWS Lambda context instance and outputs a JSON string.
36
36
 
37
37
  The input hash must look like:
38
38
 
@@ -48,6 +48,25 @@ Ama.logger.info(
48
48
  )
49
49
  ```
50
50
 
51
+ ### Ama::Logger::Formatter::Json
52
+
53
+ This formatter accepts a Ruby hash as a message and outputs a JSON string.
54
+
55
+ The input hash must look like:
56
+
57
+ ```ruby
58
+ Ama.logger.info(
59
+ event_name: 'log.info', # required
60
+ event_id: '1234', # required - indexed
61
+ event_source: 'my_source', # required - indexed
62
+ exception: 'ArgumentError - something broke', # optional - indexed
63
+ metric_name: 'error:count', # optional - indexed
64
+ metric_value: 1, # optional - indexed, coerced to integer
65
+ metric_content: 'error', # optional - indexed, coerced to string
66
+ details: { message: 'test' } # optional - non-indexed, Hash coerced to string
67
+ )
68
+ ```
69
+
51
70
  ### Ama::Logger::Formatter::StringifiedHash
52
71
 
53
72
  This formatter accepts string message and outputs a JSON string. The formatter is able to filter sensitive data based on the input provided.
@@ -29,6 +29,12 @@ module Ama
29
29
  end
30
30
  end
31
31
 
32
+ def json(io = STDOUT, *args)
33
+ ::Logger.new(io, *args).tap do |instance|
34
+ instance.formatter = Ama::Logger::Formatter::Json.new
35
+ end
36
+ end
37
+
32
38
  def stringified_hash(base, opts = {})
33
39
  base.dup.tap do |instance|
34
40
  instance.formatter = Ama::Logger::Formatter::StringifiedHash.new(opts)
@@ -8,4 +8,5 @@ require 'securerandom'
8
8
 
9
9
  require_relative 'version'
10
10
  require_relative 'formatter/lambda'
11
+ require_relative 'formatter/json'
11
12
  require_relative 'formatter/stringified_hash'
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ama
4
+ module Logger
5
+ module Formatter
6
+ class Json
7
+ def call(severity, time, _progname, msg = {})
8
+ invalid_argument!(:msg, Hash, msg) unless msg.is_a?(Hash)
9
+
10
+ event_name = msg.fetch(:event_name) { missing_argument!(:event_name) }
11
+ event_source = msg.fetch(:event_source) { missing_argument!(:event_source) }
12
+ event_id = msg.fetch(:event_id) { missing_argument!(:event_id) }
13
+
14
+ {
15
+ exception: msg[:exception],
16
+ eventName: event_name,
17
+ eventSource: event_source.to_s,
18
+ eventId: event_id.to_s,
19
+ eventAgent: Ama::Logger::AGENT_VERSION,
20
+ details: details(msg),
21
+ indexed: indexed(msg),
22
+ severity: severity,
23
+ timestamp: time.utc.iso8601
24
+ }
25
+ .compact
26
+ .to_json
27
+ .concat("\n")
28
+ end
29
+
30
+ private
31
+
32
+ def details(data = {})
33
+ data.fetch(:details) { {} }.to_json
34
+ end
35
+
36
+ def indexed(opts = {})
37
+ data = opts.slice(:metric_name, :metric_value, :metric_content)
38
+
39
+ {
40
+ metricName: data[:metric_name]&.to_s,
41
+ metricValue: data[:metric_value]&.to_i,
42
+ metricContent: data[:metric_content]&.to_s
43
+ }
44
+ .compact
45
+ end
46
+
47
+ def missing_argument!(name)
48
+ raise ArgumentError, "must pass the `#{name}` argument"
49
+ end
50
+
51
+ def invalid_argument!(name, klass, value)
52
+ raise ArgumentError, "expected type `#{klass.inspect}` for argument `#{name}` - got `#{value.inspect}`"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ama
4
4
  module Logger
5
- VERSION = '1.1.1'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ama_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Doyle
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2019-10-21 00:00:00.000000000 Z
15
+ date: 2019-12-30 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: bundler
@@ -139,6 +139,7 @@ files:
139
139
  - bin/setup
140
140
  - lib/ama/logger.rb
141
141
  - lib/ama/logger/base.rb
142
+ - lib/ama/logger/formatter/json.rb
142
143
  - lib/ama/logger/formatter/lambda.rb
143
144
  - lib/ama/logger/formatter/stringified_hash.rb
144
145
  - lib/ama/logger/version.rb
@@ -162,8 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
163
  - !ruby/object:Gem::Version
163
164
  version: '0'
164
165
  requirements: []
165
- rubyforge_project:
166
- rubygems_version: 2.5.2.3
166
+ rubygems_version: 3.1.2
167
167
  signing_key:
168
168
  specification_version: 4
169
169
  summary: Implementation of AMA's standard logging format