diff --git a/lib/contextual_logger.rb b/lib/contextual_logger.rb index c739506..9626e4f 100644 --- a/lib/contextual_logger.rb +++ b/lib/contextual_logger.rb @@ -4,7 +4,8 @@ require 'active_support/core_ext/module/delegation' require 'json' require_relative './contextual_logger/redactor' -require_relative './contextual_logger/context/handler' +require_relative './contextual_logger/context' +require_relative './contextual_logger/context_handler' module ContextualLogger LOG_LEVEL_NAMES_TO_SEVERITY = @@ -65,7 +66,7 @@ def with_context(context) end else # If no block given, return context handler to the caller so they can call reset! themselves. - Context::Handler.new(self, previous_context) + ContextHandler.new(self, previous_context) end end diff --git a/lib/contextual_logger/context/handler.rb b/lib/contextual_logger/context.rb similarity index 70% rename from lib/contextual_logger/context/handler.rb rename to lib/contextual_logger/context.rb index 84561b4..c99492f 100644 --- a/lib/contextual_logger/context/handler.rb +++ b/lib/contextual_logger/context.rb @@ -14,16 +14,5 @@ def current_context(global_context) def current_context=(context) Thread.current[thread_context_for_logger_instance] = context.freeze end - - class Handler - def initialize(instance, previous_context) - @instance = instance - @previous_context = previous_context - end - - def reset! - @instance.current_context = @previous_context - end - end end end diff --git a/lib/contextual_logger/context_handler.rb b/lib/contextual_logger/context_handler.rb new file mode 100644 index 0000000..9999475 --- /dev/null +++ b/lib/contextual_logger/context_handler.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module ContextualLogger + class ContextHandler + def initialize(instance, previous_context) + @instance = instance + @previous_context = previous_context + end + + def reset! + @instance.current_context = @previous_context + end + end +end diff --git a/spec/lib/contextual_logger/context_handler_spec.rb b/spec/lib/contextual_logger/context_handler_spec.rb new file mode 100644 index 0000000..7070b57 --- /dev/null +++ b/spec/lib/contextual_logger/context_handler_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'contextual_logger' + +class ContextualLoggerContextHandlerSpecContainer + include ::ContextualLogger::Context +end + +RSpec.describe ContextualLogger::ContextHandler do + let(:context) { { service: { name: 'tts', description: 'TTS' }, integration: "google" } } + let(:context2) { { service: { description: 'Context 2' }, integration: "google" } } + + let(:instance) { ContextualLoggerContextHandlerSpecContainer.new } + + subject(:handler) { described_class.new(instance, context) } + + it { is_expected.to respond_to(:reset!) } + + it 'resets the thread context on reset!' do + instance.current_context = context2 + handler.reset! + + expect(instance.current_context(nil)).to eq(context) + end +end diff --git a/spec/lib/contextual_logger/context/handler_spec.rb b/spec/lib/contextual_logger/context_spec.rb similarity index 71% rename from spec/lib/contextual_logger/context/handler_spec.rb rename to spec/lib/contextual_logger/context_spec.rb index a7533c9..9b793f5 100644 --- a/spec/lib/contextual_logger/context/handler_spec.rb +++ b/spec/lib/contextual_logger/context_spec.rb @@ -3,16 +3,16 @@ require 'spec_helper' require 'contextual_logger' +class ContextualLoggerContextSpecContainer + include ::ContextualLogger::Context +end + RSpec.describe ContextualLogger::Context do let(:context) { { service: { name: 'tts', description: 'TTS' }, integration: "google" } } let(:context2) { { service: { description: 'Context 2' }, integration: "google" } } let(:context3) { { service: { name: 'Context 3' } } } - class ContextualLoggerContextContainerForSpec - include ::ContextualLogger::Context - end - - let(:instance) { ContextualLoggerContextContainerForSpec.new } + let(:instance) { ContextualLoggerContextSpecContainer.new } describe 'mixin methods' do describe 'current_context/current_context=' do @@ -34,7 +34,7 @@ class ContextualLoggerContextContainerForSpec it 'the current_context= values are separately per containing instance' do instance.current_context = context - instance2 = ContextualLoggerContextContainerForSpec.new + instance2 = ContextualLoggerContextSpecContainer.new instance2.current_context = context2 expect(instance.current_context({})).to eq(context) @@ -48,17 +48,4 @@ class ContextualLoggerContextContainerForSpec end end end - - describe ContextualLogger::Context::Handler do - subject(:handler) { described_class.new(instance, context) } - - it { is_expected.to respond_to(:reset!) } - - it 'resets the thread context on reset!' do - instance.current_context = context2 - handler.reset! - - expect(instance.current_context(nil)).to eq(context) - end - end end diff --git a/spec/lib/contextual_logger_spec.rb b/spec/lib/contextual_logger_spec.rb index 6de0bd0..1cef5f0 100644 --- a/spec/lib/contextual_logger_spec.rb +++ b/spec/lib/contextual_logger_spec.rb @@ -325,7 +325,7 @@ def expect_log_line_to_be_written(log_line) end it 'returns a context handler' do - expect(logger.with_context(service: 'test_service')).to be_a(ContextualLogger::Context::Handler) + expect(logger.with_context(service: 'test_service')).to be_a(ContextualLogger::ContextHandler) end it 'prints out the wrapper context with logging' do