Skip to content

Commit

Permalink
Guard LogSubscriber against tagged logger with absent formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Jan 30, 2022
1 parent 68fb960 commit 6f7bf8a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/good_job/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def tag_logger(*tags, &block)
good_job_tag = ["ActiveJob"].freeze

self.class.loggers.inject(block) do |inner, each_logger|
if each_logger.respond_to?(:tagged)
if each_logger.respond_to?(:tagged) && each_logger.formatter
tags_for_logger = if each_logger.formatter.current_tags.include?("ActiveJob")
good_job_tag + tags
else
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/good_job/log_subscriber_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe GoodJob::LogSubscriber do
let(:subscriber) { described_class.new }

around do |example|
orig_loggers = described_class.loggers.dup
described_class.loggers.clear
described_class.reset_logger

example.run

described_class.loggers.replace(orig_loggers)
described_class.reset_logger
end

describe "loggers" do
let(:logs) { StringIO.new }

it 'logs output with a simple logger' do
described_class.loggers << Logger.new(logs)
event = ActiveSupport::Notifications::Event.new("", nil, nil, "id", {})

subscriber.scheduler_create_pool(event)
expect(logs.string).to include("GoodJob started scheduler with queues= max_threads=")
end

it 'logs output with a tagged logger with missing formatter' do
logger = ActiveSupport::TaggedLogging.new(Logger.new(logs))
logger.formatter = nil
described_class.loggers << logger

event = ActiveSupport::Notifications::Event.new("", nil, nil, "id", {})

subscriber.scheduler_create_pool(event)
expect(logs.string).to include("GoodJob started scheduler with queues= max_threads=")
end
end
end

0 comments on commit 6f7bf8a

Please sign in to comment.