Skip to content

Commit

Permalink
TECH-12593: separate Context from ContextHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Aug 31, 2023
1 parent 6c23619 commit de98517
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
5 changes: 3 additions & 2 deletions lib/contextual_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions lib/contextual_logger/context_handler.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions spec/lib/contextual_logger/context_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion spec/lib/contextual_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de98517

Please sign in to comment.