Skip to content

Commit 93a2891

Browse files
authored
Merge pull request #3839 from DataDog/lograge-rails
2 parents 1ced609 + 482b48a commit 93a2891

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/datadog/tracing/contrib/lograge/patcher.rb

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ def target_version
2020

2121
# patch applies our patch
2222
def patch
23+
# ActiveSupport::TaggedLogging is the default Rails logger since Rails 5
24+
if defined?(::ActiveSupport::TaggedLogging::Formatter) &&
25+
::Lograge::LogSubscribers::ActionController
26+
.logger&.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)
27+
28+
Datadog.logger.error(
29+
'Lograge and ActiveSupport::TaggedLogging (the default Rails log formatter) are not compatible: ' \
30+
'Lograge does not account for Rails log tags, creating polluted logs and breaking log formatting. ' \
31+
'Traces and Logs correlation may not work. ' \
32+
'Either: 1. Disable tagged logging in your Rails configuration ' \
33+
'`config.logger = ActiveSupport::Logger.new(STDOUT); ' \
34+
'config.active_job.logger = ActiveSupport::Logger.new(STDOUT)` ' \
35+
'or 2. Use the `semantic_logger` gem instead of `lograge`.'
36+
)
37+
end
38+
2339
::Lograge::LogSubscribers::Base.include(Instrumentation)
2440
end
2541
end

spec/datadog/tracing/contrib/lograge/patcher_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,31 @@
55

66
RSpec.describe Datadog::Tracing::Contrib::Lograge::Patcher do
77
describe '.patch' do
8+
before { described_class.instance_variable_get(:@patch_only_once)&.send(:reset_ran_once_state_for_tests) }
9+
810
it 'adds Instrumentation to ancestors of LogSubscribers::Base class' do
911
described_class.patch
1012

1113
expect(Lograge::LogSubscribers::Base.ancestors).to include(Datadog::Tracing::Contrib::Lograge::Instrumentation)
1214
end
15+
16+
context 'without Rails tagged logging' do
17+
it 'does not log incompatibility error' do
18+
expect(Datadog.logger).to_not receive(:error)
19+
20+
described_class.patch
21+
end
22+
end
23+
24+
context 'with Rails tagged logging' do
25+
it 'logs an incompatibility error' do
26+
logger = ActiveSupport::TaggedLogging.new(Logger.new(File::NULL))
27+
stub_const('Lograge::LogSubscribers::ActionController', double('controller', logger: logger))
28+
29+
expect(Datadog.logger).to receive(:error).with(/ActiveSupport::TaggedLogging/)
30+
31+
described_class.patch
32+
end
33+
end
1334
end
1435
end

0 commit comments

Comments
 (0)