lumberjack_json_device 2.0.0 → 2.2.0

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: 7a56676552a2828e2d6bae426aefe12de9fd0261e450f88aaba136092b22c87a
4
- data.tar.gz: c52bf71d1afd5910fbc73e3d615dd113da9ee6eb4c4b25e6e90871d00b96f363
3
+ metadata.gz: 5056428eb1c260cf68a1742064c824645ef8df0c71164a20fd8f03bb5b3f1bbc
4
+ data.tar.gz: 69ff5c7d4c88a90b5be63b9d77724b4c36f090a109791c0819bbe0f97934d0f7
5
5
  SHA512:
6
- metadata.gz: 74c584ab2ee896e681ce40a90fd49ff15191ce19fd6e77b405d3ec83af79952c146b3bd1523ce48dd239f6a4966a5d868f1dfd45edc0f40c17ad1a05a2bb805b
7
- data.tar.gz: 37e88180c62cef6d1f0548556e90ea3249f631e23c7001092cb296c9537f4d54570c4652cf09835a0dbafe2ba2b6e061b92e1a480ff10a012ec55e2c76d38982
6
+ metadata.gz: 03f44e6637e5fd9df15ccef421ebafcc035498ff272afae0c3845997bfd63099a11bc2425051fa4a94e8bf20eef7419ee961511b6fcba1a675537fc8ebaf28f3
7
+ data.tar.gz: a5a0bfdc425825484dd1cd0c95ea788919189c594cf5fb7ed55107e14655b7be55a2826f71758b2b1c93e91695c2506f84eeacc690e88919b579da339abddba0
@@ -3,13 +3,14 @@ name: Continuous Integration
3
3
  on:
4
4
  push:
5
5
  branches:
6
- - master
6
+ - main
7
7
  - actions-*
8
8
  tags:
9
9
  - v*
10
10
  pull_request:
11
11
  branches-ignore:
12
12
  - actions-*
13
+ workflow_dispatch:
13
14
 
14
15
  env:
15
16
  BUNDLE_CLEAN: "true"
@@ -30,7 +31,7 @@ jobs:
30
31
  - ruby: "2.7"
31
32
  - ruby: "2.5"
32
33
  steps:
33
- - uses: actions/checkout@v2
34
+ - uses: actions/checkout@v4
34
35
  - name: Set up Ruby
35
36
  uses: ruby/setup-ruby@v1
36
37
  with:
data/CHANGE_LOG.md CHANGED
@@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 2.2.0
8
+
9
+ ### Changed
10
+
11
+ - The `as_json` method for custom JSON serialization is now supported. This gives more consistent results when serializing non-primitive objects types. Some JSON parsing libraries and Rails will use this method, but the core `JSON` library does not which can result in unexpected behavior.
12
+ - Keep the order of keys in the payload so that the first keys are always time, severity, message, etc.
13
+ - Key mappings can now be specified using dot notation to nest attributes in the JSON document.
14
+ - Depends on Lumberjack 1.4.
15
+
16
+ ## 2.1.0
17
+
18
+ ### Changed
19
+
20
+ - Tags that contain arrays of hashes are no longer expanded to nested hashes if the hashes in the array use dot nottion in their keys. The hashes in the array will now be included as is in JSON output.
21
+
7
22
  ## 2.0.0
8
23
 
9
24
  ### Added
data/README.md CHANGED
@@ -186,7 +186,7 @@ param_filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filte
186
186
  device = Lumberjack::JsonDevice.new(STDOUT, post_processor: ->(data) { param_filter.filter(data) }
187
187
  ```
188
188
 
189
- Note that all hash keys will be strings.If the post processor does not return a hash, it will be ignored.
189
+ Note that all hash keys will be strings and the values will be JSON-safe. If the post processor does not return a hash, it will be ignored.
190
190
 
191
191
  ### Pretty Printing
192
192
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.2.0
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "lumberjack"
4
4
  require "json"
5
+ require "time"
5
6
 
6
7
  module Lumberjack
7
8
  # This Lumberjack device logs output to another device as JSON formatted text with one document per line.
@@ -160,28 +161,30 @@ module Lumberjack
160
161
  data = {}
161
162
  set_attribute(data, @time_key, entry.time) if @time_key
162
163
  set_attribute(data, @severity_key, entry.severity_label) if @severity_key
163
- set_attribute(data, @message_key, entry.message) if @message_key
164
- set_attribute(data, @progname_key, entry.progname) if @progname_key
164
+ set_attribute(data, @message_key, json_safe(entry.message)) if @message_key
165
+ set_attribute(data, @progname_key, json_safe(entry.progname)) if @progname_key && entry.progname
165
166
  set_attribute(data, @pid_key, entry.pid) if @pid_key
166
167
 
167
- tags = dereference_tags(entry.tags) if entry.tags
168
+ tags = entry.tags.transform_values { |value| json_safe(value) } if entry.tags
169
+
168
170
  extracted_tags = nil
169
171
  if @custom_keys.size > 0 && !tags&.empty?
170
172
  extracted_tags = []
171
173
  @custom_keys.each do |name, key|
172
- set_attribute(data, key, tag_value(tags, name))
173
- extracted_tags << name
174
- end
174
+ name = name.is_a?(Array) ? name.join(".") : name.to_s
175
+ value = tags.delete(name)
176
+ next if value.nil?
175
177
 
176
- extracted_tags.each do |path|
177
- tags = deep_remove_tag(tags, path, entry.tags)
178
+ value = Lumberjack::Utils.expand_tags(value) if value.is_a?(Hash)
179
+ set_attribute(data, key, value)
180
+ extracted_tags << name
178
181
  end
179
182
  end
180
183
 
181
- if @tags_key
182
- tags ||= {}
184
+ if @tags_key && !tags&.empty?
185
+ tags = Lumberjack::Utils.expand_tags(tags)
183
186
  if @tags_key == "*"
184
- data = tags.merge(data) unless tags.empty?
187
+ tags.each { |k, v| data[k] = v unless data.include?(k) }
185
188
  else
186
189
  set_attribute(data, @tags_key, tags)
187
190
  end
@@ -198,83 +201,6 @@ module Lumberjack
198
201
 
199
202
  private
200
203
 
201
- def dereference_tags(tags)
202
- updated_tags = nil
203
- remove_tags = nil
204
-
205
- tags.each do |original_key, value|
206
- updated_tags ||= tags.dup unless original_key.is_a?(String)
207
- key = original_key.to_s
208
-
209
- dot_index = key.index(".")
210
- if dot_index
211
- remove_tags ||= []
212
- remove_tags << original_key
213
- updated_tags ||= tags.dup
214
- sub_key = key[dot_index + 1..-1]
215
- key = key[0, dot_index]
216
- existing_vals = updated_tags[key]
217
- unless existing_vals.is_a?(Hash)
218
- existing_vals = {}
219
- updated_tags[key] = existing_vals
220
- end
221
- existing_vals.merge!(dereference_tags({sub_key => value}))
222
- elsif value.is_a?(Enumerable)
223
- updated_tags ||= tags.dup
224
- updated_tags[key] = if value.is_a?(Hash)
225
- dereference_tags(value)
226
- else
227
- value.collect { |v| v.is_a?(Hash) ? dereference_tags(v) : v }
228
- end
229
- elsif updated_tags
230
- updated_tags[key] = value
231
- end
232
- end
233
-
234
- return tags unless updated_tags
235
-
236
- remove_tags&.each { |key| updated_tags.delete(key) }
237
- updated_tags
238
- end
239
-
240
- def tag_value(tags, name)
241
- return nil if tags.nil?
242
- return tags[name] unless name.is_a?(Array)
243
-
244
- val = tags[name.first]
245
- return val if name.length == 1
246
- return nil unless val.is_a?(Hash)
247
-
248
- tag_value(val, name[1, name.length])
249
- end
250
-
251
- def deep_remove_tag(tags, path, original_tags)
252
- return nil if tags.nil?
253
-
254
- dup_needed = tags.equal?(original_tags)
255
- key = path.first
256
- val = tags[key] if path.length > 1
257
- unless val.is_a?(Hash)
258
- if tags.include?(key)
259
- tags = tags.dup if dup_needed
260
- tags.delete(key)
261
- end
262
- return tags
263
- end
264
-
265
- new_val = deep_remove_tag(val, path[1, path.length], original_tags[key])
266
- if new_val.empty? || !new_val.equal?(val)
267
- tags = tags.dup if dup_needed
268
- if new_val.empty?
269
- tags.delete(key)
270
- else
271
- tags[key] = new_val
272
- end
273
- end
274
-
275
- tags
276
- end
277
-
278
204
  def set_attribute(data, key, value)
279
205
  return if value.nil?
280
206
 
@@ -282,6 +208,8 @@ module Lumberjack
282
208
  value = @time_formatter.call(value)
283
209
  end
284
210
 
211
+ key = key.split(".") if key.is_a?(String) && key.include?(".")
212
+
285
213
  if key.is_a?(Array)
286
214
  unless key.empty?
287
215
  if key.size == 1
@@ -297,7 +225,7 @@ module Lumberjack
297
225
  deep_merge!(data, Lumberjack::Tags.stringify_keys(hash))
298
226
  end
299
227
  else
300
- data[key] = value unless key.nil?
228
+ data[key.to_s] = value unless key.nil?
301
229
  end
302
230
  end
303
231
 
@@ -334,5 +262,22 @@ module Lumberjack
334
262
  end
335
263
  end
336
264
  end
265
+
266
+ def json_safe(value)
267
+ return nil if value.nil?
268
+
269
+ # Check if the as_json method is defined takes no parameters
270
+ as_json_arity = value.method(:as_json).arity if value.respond_to?(:as_json)
271
+
272
+ if as_json_arity == 0 || as_json_arity == -1
273
+ value.as_json
274
+ elsif value.is_a?(Hash)
275
+ value.transform_values { |v| json_safe(v) }
276
+ elsif value.is_a?(Enumerable)
277
+ value.collect { |v| json_safe(v) }
278
+ else
279
+ value
280
+ end
281
+ end
337
282
  end
338
283
  end
@@ -28,5 +28,5 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  spec.required_ruby_version = ">= 2.5"
30
30
 
31
- spec.add_dependency "lumberjack", ">=1.3"
31
+ spec.add_dependency "lumberjack", ">=1.4"
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lumberjack_json_device
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-19 00:00:00.000000000 Z
11
+ date: 2025-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lumberjack
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '1.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '1.4'
27
27
  description:
28
28
  email:
29
29
  - bbdurand@gmail.com