betterlog 2.5.2 → 2.6.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: dee8ddf236e84f9bb10efe2ec90c12bde0ec5c61e43e061ce366b0066996dc5c
4
- data.tar.gz: 846ce5cd50e4288958f1c5025e6abeb4a34a61c983785319083b1b9183e68c28
3
+ metadata.gz: 7fc231f9eaeb5d502cb9ba3d867c4ecf03c886c454dc73d20a68aa09d01d681e
4
+ data.tar.gz: cb39e604e93e4ad9af4b3ccd8d7faaf8f0a5540597b9135ab3f8c084593353ec
5
5
  SHA512:
6
- metadata.gz: 214f53f6de4747ad217cb7ff5d082e75ccd32b76909c00a3016dc332e81afa172ca716afc5a074d2293f317ee994a193d89d4bc0dd6a86c351d00c52e6996ad4
7
- data.tar.gz: 11379f7e260a02cdea8c901667683b01bb0ac8a71d700e98e085830a5dddf554bd507225cf294ac59cacba8e062507d940e70cc8bb495ba902c37e8befabb172
6
+ metadata.gz: 57caf38f3548fa7657dfd052d0af2f97d1b56db40658352c894b69d99cccea3b34f959d6332a73370eb5c69cccd946bb4d0e2838b68ee8dd82b10df3ef56bc06
7
+ data.tar.gz: ca78626c321df508c5c15221fd0380120527f845b51788874df309d4789fdd72f2e439778f01b44d0b21ef24e3ee61f5ab2bfe185a15396c7c9184e5ed863ec9
data/CHANGES.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-12-03 v2.6.0
4
+
5
+ ### Improvements
6
+ - Moved deep key symbolization and circular reference handling from ingress to
7
+ egress in `Betterlog::Log::Event`.
8
+ - `Event#as_json` now performs the deep `symbolize_keys_recursive(circular:
9
+ :circular)` walk and key sorting, memoizing the result in `@as_json`.
10
+ - `Event#[]=` invalidates the `@as_json` cache.
11
+ - `Event#hash` and `Event#eql?` now route through `as_json`, inheriting
12
+ cycle-safety.
13
+ - Ingress methods (`Event.ify`, `Event#initialize`, `GlobalMetadata#add`, and
14
+ Sidekiq context merging) simplified to shallow `transform_keys(&:to_sym)`.
15
+ - Added `.rspec` configuration to globally load `spec_helper` and enable color
16
+ output and backtraces.
17
+ - Removed redundant `require 'spec_helper'` statements from individual spec
18
+ files.
19
+ - Removed `require 'rspec'` from `spec/spec_helper.rb`.
20
+
21
+ ### Testing
22
+ - Added specs for `Event#as_json` memoization, cache invalidation via `[]=`,
23
+ sorted keys, and deep symbolization.
24
+ - Added specs for handling circular references via `[]=` and `GlobalMetadata`.
25
+ - Added specs for `Event.ify` key normalization.
26
+ - Added specs for `Event#eql?` and `Event#hash` with circular references.
27
+ - Updated `legacy_event_formatter_spec.rb` to use `EOT` as the heredoc
28
+ delimiter to avoid collision with the Ruby `end` keyword.
29
+
3
30
  ## 2026-11-23 v2.5.2
4
31
 
5
32
  ### Build & CI
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.5.2
1
+ 2.6.0
data/betterlog.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: betterlog 2.5.2 ruby lib
2
+ # stub: betterlog 2.6.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "betterlog".freeze
6
- s.version = "2.5.2".freeze
6
+ s.version = "2.6.0".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
@@ -2,7 +2,6 @@
2
2
  # to logging and debugging tools. In addition to holding the data
3
3
  # thread-global, this will also attempt to update current of error reporting
4
4
  # tools etc.
5
-
6
5
  module Betterlog
7
6
 
8
7
  # Thread-local storage for global metadata used to enrich log events.
@@ -29,7 +28,7 @@ module Betterlog
29
28
  # @param data [ Hash ] A hash containing the metadata to be added
30
29
  # @return [ Betterlog::GlobalMetadata ] Returns the GlobalMetadata instance itself
31
30
  def add(data)
32
- data = data.symbolize_keys_recursive
31
+ data = data.to_h.transform_keys(&:to_sym)
33
32
  self.current = data | current
34
33
  self
35
34
  end
@@ -29,7 +29,7 @@ module Betterlog
29
29
  # @note Additional keyword arguments are passed through into the event data as-is.
30
30
  # @return [ Betterlog::Log::Event ] a new log event instance created from the input argument
31
31
  def self.ify(arg, **rest)
32
- rest = rest.symbolize_keys_recursive(circular: :circular)
32
+ rest = rest.transform_keys(&:to_sym)
33
33
  if e = arg.ask_and_send(:exception)
34
34
  new(
35
35
  {
@@ -41,7 +41,7 @@ module Betterlog
41
41
  elsif message = arg.ask_and_send(:to_str)
42
42
  new({ message: } | rest)
43
43
  elsif hash = arg.ask_and_send(:to_hash)
44
- new(hash | rest)
44
+ new(hash.transform_keys(&:to_sym) | rest)
45
45
  else
46
46
  message = "Logging #{arg.inspect}"
47
47
  new({ message: } | rest)
@@ -96,7 +96,7 @@ module Betterlog
96
96
  # @param data [ Hash ] a hash containing the initial data for the log event
97
97
  # @return [ Betterlog::Log::Event ] a new log event instance with processed data
98
98
  def initialize(data = {})
99
- data = compute_data(data.symbolize_keys_recursive(circular: :circular))
99
+ data = data.to_h.transform_keys(&:to_sym)
100
100
  data[:severity] =
101
101
  begin
102
102
  Severity.new((data[:severity] || :debug))
@@ -106,7 +106,8 @@ module Betterlog
106
106
  unless data.key?(:message)
107
107
  data[:message] = "a #{data[:type]} type log message of severity #{data[:severity]}"
108
108
  end
109
- @data = Hash[data.sort_by(&:first)]
109
+ @data = compute_data(data)
110
+ @as_json = nil
110
111
  end
111
112
 
112
113
  # Returns a duplicate of the internal data hash for JSON serialization.
@@ -120,7 +121,7 @@ module Betterlog
120
121
  # @return [ Hash ] a duplicate of the internal data hash containing all
121
122
  # log event attributes and metadata
122
123
  def as_json(*a)
123
- @data.dup
124
+ @as_json ||= Hash[@data.symbolize_keys_recursive(circular: :circular).sort_by(&:first)]
124
125
  end
125
126
 
126
127
  # Converts the log event to a JSON string representation.
@@ -173,6 +174,7 @@ module Betterlog
173
174
  # @return [ Object ] the assigned value
174
175
  def []=(name, value)
175
176
  @data[name.to_sym] = value
177
+ @as_json = nil
176
178
  end
177
179
 
178
180
  # Retrieves a value from the event data hash using the provided name as a key.
@@ -240,7 +242,7 @@ module Betterlog
240
242
  # @return [ TrueClass, FalseClass ] true if both objects have equal internal data,
241
243
  # false otherwise
242
244
  def eql?(other)
243
- @data.eql? other.instance_variable_get(:@data)
245
+ as_json.eql? other.as_json
244
246
  end
245
247
 
246
248
  alias == eql?
@@ -255,7 +257,7 @@ module Betterlog
255
257
  #
256
258
  # @return [ Integer ] the hash value of the event's internal data structure
257
259
  def hash
258
- @data.hash
260
+ as_json.hash
259
261
  end
260
262
 
261
263
  private
@@ -283,7 +285,7 @@ module Betterlog
283
285
  }
284
286
  d[:meta] ||= {}
285
287
  d[:meta] |= (GlobalMetadata.current |
286
- (Sidekiq::Context.current&.symbolize_keys_recursive if defined?(Sidekiq::Context)))
288
+ (Sidekiq::Context.current&.transform_keys(&:to_sym) if defined?(Sidekiq::Context)))
287
289
  d
288
290
  end
289
291
  end
@@ -5,7 +5,7 @@
5
5
  # by hand; it is rewritten on every `rake version` invocation.
6
6
  module Betterlog
7
7
  # The current version of the Betterlog gem as a String.
8
- VERSION = '2.5.2'
8
+ VERSION = '2.6.0'
9
9
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
10
10
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
11
11
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe Betterlog::GlobalMetadata do
4
2
  describe '.add' do
5
3
  it 'can add to context' do
@@ -1,11 +1,32 @@
1
- require 'spec_helper'
2
-
3
1
  describe Betterlog::Log::Event do
4
2
  describe '#as_json' do
5
3
  it 'dups hash before returning it' do
6
4
  event = described_class.new
7
5
  expect(event.as_json).not_to equal event.instance_variable_get(:@data)
8
6
  end
7
+
8
+ it 'memoizes the result' do
9
+ event = described_class.new(foo: 'bar')
10
+ expect(event.as_json).to be(event.as_json)
11
+ end
12
+
13
+ it 'invalidates memoization on []=' do
14
+ event = described_class.new(foo: 'bar')
15
+ first = event.as_json
16
+ event[:baz] = 'qux'
17
+ expect(event.as_json).not_to be(first)
18
+ expect(event.as_json[:baz]).to eq('qux')
19
+ end
20
+
21
+ it 'returns keys in sorted order' do
22
+ event = described_class.new(zeta: 1, alpha: 2, mid: 3)
23
+ expect(event.as_json.keys).to eq(event.as_json.keys.sort)
24
+ end
25
+
26
+ it 'deep-symbols nested string keys' do
27
+ event = described_class.new(nested: { 'inner' => 'value' })
28
+ expect(event.as_json[:nested]).to eq({ inner: 'value' })
29
+ end
9
30
  end
10
31
 
11
32
  describe '#to_json' do
@@ -24,9 +45,56 @@ describe Betterlog::Log::Event do
24
45
  it 'does not replace repeated scalar objects with "circular"' do
25
46
  ary = [true, false, nil, 23, 3.14]
26
47
  event = described_class.new(ary: ary << ary.dup)
27
- expect(event.to_json).to include(<<~end.strip)
48
+ expect(event.to_json).to include(<<~EOT.strip)
28
49
  "ary":[true,false,null,23,3.14,[true,false,null,23,3.14]]
29
- end
50
+ EOT
51
+ end
52
+
53
+ it 'can deal with circular arrays set via []=' do
54
+ event = described_class.new
55
+ circular = [].tap { |a| a << a }
56
+ event[:circular] = circular
57
+ expect(event.to_json).to include('"circular":["circular"]')
58
+ end
59
+
60
+ it 'can deal with circular hashes from GlobalMetadata' do
61
+ circular = {}.tap { |h| h[:self] = h }
62
+ Betterlog::GlobalMetadata.add(circ: circular)
63
+ event = described_class.new
64
+ expect(event.to_json).to include('"circ":{"self":"circular"}')
65
+ ensure
66
+ Betterlog::GlobalMetadata.remove([:circ])
67
+ end
68
+ end
69
+
70
+ describe '.ify' do
71
+ it 'symbolizes string keys in kwargs' do
72
+ event = described_class.ify('hello', 'user_id' => 42)
73
+ expect(event[:user_id]).to eq(42)
74
+ end
75
+
76
+ it 'symbolizes string keys in hash argument' do
77
+ event = described_class.ify({ 'msg' => 'hi', 'id' => 1 })
78
+ expect(event[:msg]).to eq('hi')
79
+ expect(event[:id]).to eq(1)
80
+ end
81
+ end
82
+
83
+ describe '#eql?' do
84
+ it 'does not raise on circular refs' do
85
+ a = described_class.new
86
+ circ = [].tap { |x| x << x }
87
+ a[:circ] = circ
88
+ expect { a == a }.not_to raise_error
89
+ end
90
+ end
91
+
92
+ describe '#hash' do
93
+ it 'does not raise on circular refs' do
94
+ a = described_class.new
95
+ circ = [].tap { |x| x << x }
96
+ a[:circ] = circ
97
+ expect { a.hash }.not_to raise_error
30
98
  end
31
99
  end
32
100
  end
@@ -1,4 +1,3 @@
1
- require 'spec_helper'
2
1
  module ActiveSupport
3
2
  class Logger
4
3
  class Formatter
@@ -31,11 +30,11 @@ describe Betterlog::Log::LegacyEventFormatter do
31
30
  end
32
31
 
33
32
  it 'deconstructs backtraces' do
34
- msg = <<~end
33
+ msg = <<~EOT
35
34
  foo.rb:6:in `bar': hi (RuntimeError)
36
35
  from foo.rb:2:in `foo'
37
36
  from foo.rb:9:in `<main>'
38
- end
37
+ EOT
39
38
  json = described_class.new.(:info, Time.now, 'foo', msg)
40
39
  data = JSON.parse(json)
41
40
  expect(data['message']).to eq "foo.rb:6:in `bar': hi (RuntimeError)\n"
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe Betterlog::Log::Severity do
4
2
  it 'has all constants' do
5
3
  expect(described_class.all.map(&:to_s).sort).to\
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe Betterlog::Log do
4
2
  around do |example|
5
3
  Time.dummy '2011-11-11 11:11:11+01:00' do
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe 'Betterlog::VERSION' do
4
2
  it 'has one' do
5
3
  expect(Betterlog::VERSION).to be_a String
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'gem_hadar/simplecov'
2
2
  GemHadar::SimpleCov.start
3
- require 'rspec'
4
3
  begin
5
4
  require 'debug'
6
5
  rescue LoadError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.2
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - betterplace Developers