Skip to content

Update lograge/patcher.rb so that the correct logger is checked #4496

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions lib/datadog/tracing/contrib/lograge/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ def target_version

# patch applies our patch
def patch
# First check Lograge logger directly for when keep_original_rails_log option is used
used_logger = ::Lograge.logger || ::Lograge::LogSubscribers::ActionController.logger

# ActiveSupport::TaggedLogging is the default Rails logger since Rails 5
if defined?(::ActiveSupport::TaggedLogging::Formatter) &&
::Lograge::LogSubscribers::ActionController
.logger&.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)
used_logger&.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)
Datadog.logger.warn(
'Lograge and ActiveSupport::TaggedLogging (the default Rails log formatter) are not compatible: ' \
'Lograge does not account for Rails log tags, creating polluted logs and breaking log formatting. ' \
Expand Down
30 changes: 29 additions & 1 deletion spec/datadog/tracing/contrib/lograge/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,43 @@
end
end

context 'with Rails tagged logging' do
context 'with tagged logging for the Lograge logger' do
before do
logger = ActiveSupport::TaggedLogging.new(Logger.new(File::NULL))
allow(::Lograge).to receive(:logger).and_return(logger)
end

it 'logs an incompatibility error' do
expect(Datadog.logger).to receive(:warn).with(/ActiveSupport::TaggedLogging/)

described_class.patch
end
end

context 'with tagged logging for the Rails logger' do
before do
logger = ActiveSupport::TaggedLogging.new(Logger.new(File::NULL))
stub_const('Lograge::LogSubscribers::ActionController', double('controller', logger: logger))
end

it 'logs an incompatibility error' do
expect(Datadog.logger).to receive(:warn).with(/ActiveSupport::TaggedLogging/)

described_class.patch
end

context "when the Lograge logger does not use tagged logging" do
before do
logger = ActiveSupport::Logger.new(File::NULL)
allow(::Lograge).to receive(:logger).and_return(logger)
end

it 'does not log incompatibility error' do
expect(Datadog.logger).to_not receive(:warn)

described_class.patch
end
end
end
end
end
Loading