lumberjack_json_device 2.1.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: 81b9e1a335053608bb8428db2f6828acd748c02ec0610c0c064e060ab94fe36c
4
- data.tar.gz: 9a1bf5cb6cce667cde7837624be8db44826976abb4c07c9568481d89efd38c16
3
+ metadata.gz: 5056428eb1c260cf68a1742064c824645ef8df0c71164a20fd8f03bb5b3f1bbc
4
+ data.tar.gz: 69ff5c7d4c88a90b5be63b9d77724b4c36f090a109791c0819bbe0f97934d0f7
5
5
  SHA512:
6
- metadata.gz: 3226afbeeb3c72a0e60b940e26fa47973882d6f5770dff91d0d859087c0399a033c1dd54982da74a03fddd8e8fc7acc2849773b0a20bf362d1c4551314a5ba4e
7
- data.tar.gz: 2068343690c36690deda5c7c6b91d9c9f1ba8663ad375f28f064ab88d9acdb9ffdb8912b04611c4898363c024bcc01d9e6fca8ab8293a58db3a72992234ff4b7
6
+ metadata.gz: 03f44e6637e5fd9df15ccef421ebafcc035498ff272afae0c3845997bfd63099a11bc2425051fa4a94e8bf20eef7419ee961511b6fcba1a675537fc8ebaf28f3
7
+ data.tar.gz: a5a0bfdc425825484dd1cd0c95ea788919189c594cf5fb7ed55107e14655b7be55a2826f71758b2b1c93e91695c2506f84eeacc690e88919b579da339abddba0
data/CHANGE_LOG.md CHANGED
@@ -4,6 +4,15 @@ 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
+
7
16
  ## 2.1.0
8
17
 
9
18
  ### Changed
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.1.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 = Lumberjack::Utils.expand_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,44 +201,6 @@ module Lumberjack
198
201
 
199
202
  private
200
203
 
201
- def tag_value(tags, name)
202
- return nil if tags.nil?
203
- return tags[name] unless name.is_a?(Array)
204
-
205
- val = tags[name.first]
206
- return val if name.length == 1
207
- return nil unless val.is_a?(Hash)
208
-
209
- tag_value(val, name[1, name.length])
210
- end
211
-
212
- def deep_remove_tag(tags, path, original_tags)
213
- return nil if tags.nil?
214
-
215
- dup_needed = tags.equal?(original_tags)
216
- key = path.first
217
- val = tags[key] if path.length > 1
218
- unless val.is_a?(Hash)
219
- if tags.include?(key)
220
- tags = tags.dup if dup_needed
221
- tags.delete(key)
222
- end
223
- return tags
224
- end
225
-
226
- new_val = deep_remove_tag(val, path[1, path.length], original_tags[key])
227
- if new_val.empty? || !new_val.equal?(val)
228
- tags = tags.dup if dup_needed
229
- if new_val.empty?
230
- tags.delete(key)
231
- else
232
- tags[key] = new_val
233
- end
234
- end
235
-
236
- tags
237
- end
238
-
239
204
  def set_attribute(data, key, value)
240
205
  return if value.nil?
241
206
 
@@ -243,6 +208,8 @@ module Lumberjack
243
208
  value = @time_formatter.call(value)
244
209
  end
245
210
 
211
+ key = key.split(".") if key.is_a?(String) && key.include?(".")
212
+
246
213
  if key.is_a?(Array)
247
214
  unless key.empty?
248
215
  if key.size == 1
@@ -258,7 +225,7 @@ module Lumberjack
258
225
  deep_merge!(data, Lumberjack::Tags.stringify_keys(hash))
259
226
  end
260
227
  else
261
- data[key] = value unless key.nil?
228
+ data[key.to_s] = value unless key.nil?
262
229
  end
263
230
  end
264
231
 
@@ -295,5 +262,22 @@ module Lumberjack
295
262
  end
296
263
  end
297
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
298
282
  end
299
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.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.1.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-23 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.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.3
26
+ version: '1.4'
27
27
  description:
28
28
  email:
29
29
  - bbdurand@gmail.com