diff --git a/Gemfile.lock b/Gemfile.lock index 9b30026..61cb344 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - hanami-context-logging (0.1.1) + hanami-context-logging (0.1.2) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index ac86237..78ced79 100644 --- a/README.md +++ b/README.md @@ -41,35 +41,67 @@ Or install it yourself as: ## Usage ### About context_provider -context_provider is simply any object that responds to `#context` which returns a hash. The below are all valid context providers +context_provider is simply any object that responds to `#context` which returns a hash OR is convertible to a hash via #to_h. The below are all valid context providers **Struct** ``` ContextProviderStruct = Struct.new(:context) provider = ContextProviderStruct.new(a_context: 'a_value') -provider.context # returns context, all good +provider.context # returns hash, all good ``` **Class** ``` +# is a hash class ContextProviderClass def self.context { a_context: 'a_value' } end end provider = ContextProviderClass -provider.context # returns context, all good +provider.context # returns hash, all good + +# can be converted to hash via to_h +class Context + def to_h + { a_context: 'a_value' } + end +end + +class ContextProviderClass + def self.context + Context.new + end +end +provider = ContextProviderClass +provider.context.to_h # convertible to hash, all good ``` **Object** ``` +# is a hash class ContextProviderClass def context + { a_context: 'a_value' } + end + end + provider = ContextProviderClass.new + provider.context # returns hash, all good + +# can be converted to hash via to_h +class Context + def to_h { a_context: 'a_value' } end end -provider = ContextProviderClass.new -provider.context # returns context, all good + +class ContextProviderClass + def self.context + Context.new + end +end +provider = ContextProviderClass +provider.context.to_h # convertible to hash, all good ``` The logger allows you to define your own context provider. A use case for context provider is when the context is not yet known during initialization, but will be known during logging. For example diff --git a/lib/hanami_context_logging/formatter/with_context.rb b/lib/hanami_context_logging/formatter/with_context.rb index fe5116d..904f1c2 100644 --- a/lib/hanami_context_logging/formatter/with_context.rb +++ b/lib/hanami_context_logging/formatter/with_context.rb @@ -17,8 +17,13 @@ def formatted_contexts end def contexts + provider_context = if @context_provider.context.is_a?(Hash) + @context_provider.context + else + @context_provider.context.to_h + end {}.merge( - @context_provider.context, + provider_context, @ad_hoc_context ) end diff --git a/lib/hanami_context_logging/formatter/with_context_json.rb b/lib/hanami_context_logging/formatter/with_context_json.rb index 3e417e0..9ecaefe 100644 --- a/lib/hanami_context_logging/formatter/with_context_json.rb +++ b/lib/hanami_context_logging/formatter/with_context_json.rb @@ -18,8 +18,13 @@ def _format(hash) end def contexts + provider_context = if @context_provider.context.is_a?(Hash) + @context_provider.context + else + @context_provider.context.to_h + end {}.merge( - @context_provider.context, + provider_context, @ad_hoc_context ) end diff --git a/lib/hanami_context_logging/logger.rb b/lib/hanami_context_logging/logger.rb index 1395f09..9433c8b 100644 --- a/lib/hanami_context_logging/logger.rb +++ b/lib/hanami_context_logging/logger.rb @@ -7,7 +7,8 @@ module HanamiContextLogging class Logger < Hanami::Logger # A logger that has the same interface as Hanami::Logger, except # ContextLogger accepts one more option, called :context_provider - # context_provider is just any object that responds to #context which returns a hash + # context_provider is just any object that responds to #context which returns a hash, + # OR returns an object that can be converted to hash via to_h # # We first need to extract this option, otherwise Hanami::Logger cannot be initialized due to unrecognized option # and pad the formatter to be :with_context by default diff --git a/lib/hanami_context_logging/version.rb b/lib/hanami_context_logging/version.rb index af61c01..0ef58ff 100644 --- a/lib/hanami_context_logging/version.rb +++ b/lib/hanami_context_logging/version.rb @@ -1,3 +1,3 @@ module HanamiContextLogging - VERSION = "0.1.2" + VERSION = "0.1.3" end diff --git a/spec/hanami_context_logging/logger_spec.rb b/spec/hanami_context_logging/logger_spec.rb index f2d8ef8..40a6169 100644 --- a/spec/hanami_context_logging/logger_spec.rb +++ b/spec/hanami_context_logging/logger_spec.rb @@ -16,6 +16,24 @@ stream.rewind expect(stream.read).to include('any_key_1=any_value_1', 'any_key_2=any_value_2', 'random message') end + + context 'when context_provider#context is an object that responds to #to_h' do + let(:mock_context) do + double(to_h: + { + any_key_1: 'any_value_1', + any_key_2: 'any_value_2' + } + ) + end + + it 'logs message including the context given from provider' do + logger.info 'random message' + + stream.rewind + expect(stream.read).to include('any_key_1=any_value_1', 'any_key_2=any_value_2', 'random message') + end + end end describe 'with with_context_json formatter' do @@ -26,6 +44,24 @@ stream.rewind expect(JSON.parse(stream.read)).to include('any_key_1' => 'any_value_1', 'any_key_2' => 'any_value_2', 'message' => 'random message') end + + context 'when context_provider#context is an object that responds to #to_h' do + let(:mock_context) do + double(to_h: + { + any_key_1: 'any_value_1', + any_key_2: 'any_value_2' + } + ) + end + + it 'logs message including the context given from provider' do + logger.info 'random message' + + stream.rewind + expect(JSON.parse(stream.read)).to include('any_key_1' => 'any_value_1', 'any_key_2' => 'any_value_2', 'message' => 'random message') + end + end end describe '#with_context' do