Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve context when adding a new context provider #89

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/hash_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module HashExtensions
def to_nested
self unless contains_dotted_key?
return self unless contains_dotted_key?

keys.reduce({}) do |nested, key|
nested.deep_merge(build_nested_object(key, self[key]))
Expand Down
8 changes: 7 additions & 1 deletion lib/twiglet/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ def with(default_properties)
end

def context_provider(&blk)
new_context_provider = blk
if @args[:context_provider]
new_context_provider = lambda do
@args[:context_provider].call.merge(blk.call)
end
end
self.class.new(
@service_name,
**@args.merge(context_provider: blk)
**@args.merge(context_provider: new_context_provider)
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/twiglet/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(msg)
when Hash
replace(msg.transform_keys!(&:to_sym))
else
super(msg)
super
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twiglet/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Twiglet
VERSION = '3.9.2'
VERSION = '3.10.0'
end
25 changes: 23 additions & 2 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'minitest/autorun'
require 'minitest/mock'
require_relative '../lib/twiglet/logger'
require 'active_support'

LEVELS = [
{ method: :debug, level: 'debug' },
Expand Down Expand Up @@ -266,6 +267,27 @@
assert_equal 'my-context-id', log[:context][:id]
end

it "previously supplied context providers should be preserved" do
# Let's add some context to this customer journey
purchase_logger = @logger
.context_provider { { 'first-context' => { 'first-id' => 'my-first-context-id' } } }
.context_provider { { 'second-context' => { 'second-id' => 'my-second-context-id' } } }
# do stuff
purchase_logger.info(
{
message: 'customer bought a dog',
pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
}
)

log = read_json @buffer

assert_equal 'customer bought a dog', log[:message]
assert_equal 'Barker', log[:pet][:name]
assert_equal 'my-first-context-id', log[:'first-context'][:'first-id']
assert_equal 'my-second-context-id', log[:'second-context'][:'second-id']
end

it "should log 'message' string property" do
message = {}
message['message'] = 'Guinea pigs arrived'
Expand Down Expand Up @@ -393,8 +415,7 @@
assert_equal 'Some error', actual_log[:message]
end

it 'should log error type properly even when active_support/json is required' do
require 'active_support/json'
it 'should log error type properly even when active_support is required' do
e = StandardError.new('Unknown error')
@logger.error('Artificially raised exception with string message', e)

Expand Down
2 changes: 1 addition & 1 deletion twiglet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |gem|

gem.license = 'Copyright SimplyBusiness'

gem.add_runtime_dependency 'json-schema'
gem.add_dependency 'json-schema'
gem.add_development_dependency 'minitest'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'simplecov', '0.17.1'
Expand Down
Loading